Link to home
Start Free TrialLog in
Avatar of Countryboy2
Countryboy2

asked on

batch file to check file size

Hello,

I want to write a DOS batch file that will check the size of a file and if it is above a certain size then execute another command. I was thinking along the lines of using the IF function to check the file size. I don't know if it can be done.
Thanks
Avatar of K_2K
K_2K

Yes, if you have full command extensions, using FOR /F to get the size from a DIR /-C listing.
(\o/)
ASKER CERTIFIED SOLUTION
Avatar of K_2K
K_2K

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
Avatar of Countryboy2

ASKER

Thanks for that, but it only seems to run the "bigfile.cmd", I can't seem to get it to run the other file.
This is what I tried:

::-----Size_of_files.cmd-----Start
@ECHO off
:: Size_of_files.cmd
:: Run a different command based on size of a file

:: Tell cmd not to keep variables:
SETLOCAL ENABLEEXTENSIONS

:: set file name to watch
SET file=c:\test\temp.txt
:: set size threshhold in bytes
SET size=16384999

::Get directory entry and run routine if size on file dir list
FOR /F "TOKENS=4 SKIP=5" %%K IN ('DIR /-C %file%') DO CALL :sizechk %%K
GOTO :end

:: Use first argument or win.ini for source file
:sizechk
IF '%1=='bytes GOTO :EOF
IF '%1==' GOTO :EOF
echo Testing size of %file%:
IF %1 GTR %size% (
 echo %1 bytes is over the threshold of %size% bytes.  
 echo Remove remark in :sizechk to run bigfile.cmd
CALL "C:\Data\Backup log files copy batch file\acacianetsendgood.bat"
) ELSE (
 echo %1 bytes is not over the threshold of %size% bytes.  
 echo Remove remark in :sizechk to run smallfile.cmd
CALL "C:\Data\Backup log files copy batch file\acacianetsendbad.bat"
)
:: goto :EOF should return to for loop and check other files that match %file%
goto :EOF


:end
:: ENDLOCAL is implied if at end of script.
ENDLOCAL
::-----Size_of_files.cmd-----END
Hang on, seems to be working now. It was the Tokens=4 entry, I need to use Tokens=3 for it to pick up the file size. However, it is running the process 3 times, even when there is only one file in the target directoy.
But thanks for your help here.
Sry for the late reply.

The three runs is a bad thing.

Hmmm,  I based the logic on my result of DIR command, using the first two IF statements in :sizechk to filter out the line that has the word "byte".  Enter the following at a DOS prompt and maybe you'll see where the other two runs are coming from:
     DIR /-C c:\test\temp.txt

Mine shows some skipped lines and the following:
06/05/2003  12:54 AM                 0 temp.txt
               3 File(s)            169 bytes
               0 Dir(s)  81,268,502,528 bytes free


If your DIR is in the same format as mine, and your test file is empty or short,  it should not be running acacianetsendgood.bat at all when %%K is the 4th word (token).   Token=3 would compare "AM" or "PM" which is always greater than any number, so it runs acacianetsendgood.bat.  Then it would compare the number (169) from the total bytes line, so sometimes run either good or bad.  Then it would compare the number from the bytes free line, probably always running acacianetsendgood.bat again, unless your hard drive is nearly full.

The important thing is to make sure the 3rd word on the DIR line really is your file size and not something else, and add a filter for third word of any line you don't want to check. the third word of the above two last lines could be filtered out like this: (not tested)

:: Use first argument
:sizechk
IF '%1=='bytes GOTO :EOF
IF "%1"=="File(s)" GOTO :EOF
IF "%1"=="Dir(s)" GOTO :EOF
.
.
.


Of course,  you don't need a acacianetsendbad.bat if it does nothing,  I just threw smallfile.cmd in for those who google-find this and need it.

Let me know if that's not enough to help finish the thing.

Enjoy,

2K
(\o/)

Yep, my DIR command is slightly different to yours (as below)

05/06/2003  01:40p                6084 temp.txt
               1 File(s)           6084 bytes
               2 Dir(s)      1800003072 bytes free

So for me it did need Tokens=3.

I managed to get round the fact that it was running it 3 times by just sticking an 'exit' after the second call, not pretty but it did the job.

Thanks for your help with this one, very much appreciated.