Discussion Locked

Getting A File Size in A Batch Program

0 helpful
5 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.
I would like someone to assist me on how to retrieve the size of any textfile in a batch program such that the value so obtained will be referenced (in an if command) to decide whether or not to process the file.

5 replies

by seagull data services

I don't think this can be done if you're using DOS. In windows scripting host you can use the filesystemobject to return file sizes. There are a few recent articles on this site about windows scripting.

by beermonster

I'm not sure about 95/98, but this is definitely achievable in NT and win2k - I've used it to run on the fly disk space checks. an example is
for /F "skip=3 tokens=3,4" %%A in ('dir *.txt /-C') do call :go %%A %%B
rem NOTE the /-C tells dir not toput commas in the file size
goto end
:go
set /a size=%1
rem NOTE that the /a tells it that the variable is a number
IF %size% EQU 10000 echo %2 is bigger than 10000
rem NOTE other operators are LSS GTR NEQ
:end

This is only a suggested outline for your script. The dir command also returns a total bytes line that will be picked up which you will need to trap and ignore. I would suggest using
set fname=%2
IF /I %fname%=="bytes" goto end
Note that using %1 and %2 directly to do some processing can be flakey, which is why I have used the fname variable. If you need any help when writing your specific script then mail me at darthurs@hotmail.com

This discussion is closed to new replies.