Link to home
Start Free TrialLog in
Avatar of bginfosys
bginfosys

asked on

Bat file size check

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.  
Avatar of knightEknight
knightEknight
Flag of United States of America image

@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
Avatar of Bill Prew
Bill Prew

How large can the files be (maximum)?

~bp
Avatar of bginfosys

ASKER

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?  
ASKER CERTIFIED SOLUTION
Avatar of knightEknight
knightEknight
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
==> bginfosys:

==> They can grow

Can they grow over 2GB ?  :-)

~bp
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.
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%\"
      )
   )
)
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"
Apologies for all the posts... Just trying to help!
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
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!
Drat!

:)