Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Bat file size check

Posted on 2010-09-24
12
1,477 Views
Last Modified: 2012-05-10
I have a batch file that moves a file from one directory to another, daily.  I need to add a size check before the file is moved.  It needs to perform a size check and say it is above 80MB, proceed with my script to move the file.  If it is below 80MB, execute a VB script which I have ready and terminate the bat file.  
0
Comment
Question by:bginfosys
  • 6
  • 3
  • 2
  • +1
12 Comments
 
LVL 33

Expert Comment

by:knightEknight
ID: 33753936
@echo off
setlocal enabledelayedexpansion

cd \my_folder

for %%F in (myfilename.ext) do set/a myfilesize=%%~zF

if !myfilesize! GEQ 80000000 goto :movethefile

"my_vb_script.vbs"
exit/b

 :movethefile

move myfilename.ext  \someotherfolder
0
 
LVL 53

Expert Comment

by:Bill Prew
ID: 33755073
How large can the files be (maximum)?

~bp
0
 

Author Comment

by:bginfosys
ID: 33756839
They can grow.  I'm testing the script right now.  Can you also add that if there is no file in the directory to also move the file and execute the vbs file?  
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 33

Accepted Solution

by:
knightEknight earned 500 total points
ID: 33756888
My script was only intended as an example of how to check the file size and act depending on whether is was over or under 80MB.  Hopefully you would be able to incorporate this into your existing script.

Please clarify what you mean by this:  "if there is no file in the directory to also move the file and execute the vbs file"

If there is no file in the directory, then there is nothing to move...

Below is a modified version that will run the VB script the file does not exist:

---------------------------------

@echo off
setlocal enabledelayedexpansion

cd \my_folder

if not exist myfilename.ext goto :runvb

for %%F in (myfilename.ext) do set/a myfilesize=%%~zF

if !myfilesize! GEQ 80000000 goto :movethefile

:runvb

"my_vb_script.vbs"
exit/b

 :movethefile

move myfilename.ext  \someotherfolder


0
 
LVL 53

Expert Comment

by:Bill Prew
ID: 33756941
==> bginfosys:

==> They can grow

Can they grow over 2GB ?  :-)

~bp
0
 
LVL 16

Expert Comment

by:t0t0
ID: 33768204
Actually, 80GB is in fact 83886080 bytes sot the comparison should be as foillows:

   IF %%~zF GTR 83886080 (
      MOVE filename.ext
   )


Interestingly enough, you have stated a very common error by saying:

   "(if)...it is above 80MB, proceed with my script to move the file..."

   "If it is below 80MB, execute a VB script..."

Niether of these two condition account for the filesize being EXACTLY 80GB. They test for 'ABOVE' and 'BELOW'. How would you want the file processed if it is exactly 80GB?

I see knightEknight assumes the following logic:

    if !myfilesize! GEQ 80000000....

However, it could just as well have also been written as:

    if !myfilesize! LEQ 80000000...

Or, it could even be:

    if !myfilesize! EQU 80000000 (
        REM process file if it's exactly 80GB
    ) else (
        if  !myfilesize! LSS 80000000 (
           REM process file if it's smaller than 80GB
        ) else (
           REM process file if it's larger than 80GB
        )
    )


Thses are just my observations.
0
 
LVL 16

Expert Comment

by:t0t0
ID: 33768397
As stated above, 80GB is actually 83886080 bytes (not 80000000).

This is how I would code it:

NOTE: You will need to change the first 3 lines to point to your own file / folder names.


@echo off
set source=c:\source_folder
set destination=c:\destination_folder
set file=filename.ext
set size=83886080

if not exist "%destination%\%file%" (
   move "%source%\%file%" "%destination%\"
   "run_script.vbs"
) else (
   for %%f in ("source%\%file%") do (
      if %%~zf leq %size% (
         "run_script.vbs"
      ) else (
         move /y "%source%\%file%" "%destination%\"
      )
   )
)
0
 
LVL 16

Expert Comment

by:t0t0
ID: 33768425
Here's another style:

@echo off
set source=c:\source_folder
set destination=c:\destination_folder
set file=filename.ext
set size=83886080

if not exist "%destination%\%file%" (
   move "%source%\%file%" "%destination%\"
   "run_script.vbs"
   goto :eof
)

for %%f in ("source%\%file%") do set filesize=%%~zf

if %filesize% geq %size% (
   move /y "%source%\%file%" "%destination%\"
   goto :eof
)

"run_script.vbs"
0
 
LVL 16

Expert Comment

by:t0t0
ID: 33768431
Apologies for all the posts... Just trying to help!
0
 
LVL 53

Expert Comment

by:Bill Prew
ID: 33768751
I think every place t0t0 said 80GM he meant 80MB.

And just in case it's not known to the poster.  The reason I have asked about the maximum potential file size is because the largest number a BAT script can easily deal with is 2147483647.  That's just about 2GB.  So if your file can be larger than 2GB the techniques we have shown in this thread will fail, and we need to look at other approaches.

FWIW, when I need a large constant like 80MB in a BAT script I typically calculate it inline for clarity, so would have something like this near the top:

set /A 80MB=80*1024*1024

then in the script when needed I refer to that value as either

%80MB%
!80MB!

depending on the context.

~bp
0
 
LVL 16

Expert Comment

by:t0t0
ID: 33769744
Ah Bill, you are dead right... I did mean 80MB, NOT 80GB. How silly of me to shoot myself in the foot after pointing out the 80000000 vs 83886080 thing!

I stand to be corrected and therefore, all references to '80GB' in my posts above shuld infact be amended to '80MB'.

Bill, the 2GB thing is a good one... although, it's something I've never considered in the past because I naturally assumed files greater than 2GB can be COPYed, MOVEd, XCOPYed, DELeted, RENamed etc...

I think what you're refering to is just the numerical value (from a mathematical point of view). The following maths will work:

   set /a 2147483648 - 1
   2147483647

whereas, increasing the number past this threshold doesn't - and DOS reverts to negative results.

   set /a 2147483649 - 1
   -2147483648

So, testing for filesizes greater than 2GB is a problem.

Good point!
0
 
LVL 16

Expert Comment

by:t0t0
ID: 33825910
Drat!

:)
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Synchronize a new Active Directory domain with an existing Office 365 tenant
The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question