Solved

Bat file size check

Posted on 2010-09-24
12
1,443 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 51

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
 
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 51

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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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 51

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

How to sign a powershell script so you can prevent tampering, and only allow users to run authorised Powershell scripts
Set OWA language and time zone in Exchange for individuals, all users or per database.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

758 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now