• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1543
  • Last Modified:

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.  
0
bginfosys
Asked:
bginfosys
  • 6
  • 3
  • 2
  • +1
1 Solution
 
knightEknightCommented:
@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
 
Bill PrewCommented:
How large can the files be (maximum)?

~bp
0
 
bginfosysAuthor Commented:
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
Simplify Active Directory Administration

Administration of Active Directory does not have to be hard.  Too often what should be a simple task is made more difficult than it needs to be.The solution?  Hyena from SystemTools Software.  With ease-of-use as well as powerful importing and bulk updating capabilities.

 
knightEknightCommented:
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
 
Bill PrewCommented:
==> bginfosys:

==> They can grow

Can they grow over 2GB ?  :-)

~bp
0
 
t0t0Commented:
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
 
t0t0Commented:
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
 
t0t0Commented:
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
 
t0t0Commented:
Apologies for all the posts... Just trying to help!
0
 
Bill PrewCommented:
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
 
t0t0Commented:
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
 
t0t0Commented:
Drat!

:)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 6
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now