Rev
Rev

Reputation: 43

Possible to Check File Size Before Issuing Command?

I am looking for a way to test the file "resume.txt", before executing the attached if/else statement. Is there a way to check and see if the file exsists and if its size is > 0KB? ... I want to exit the program if the file is =< 0KB. Is this possible?

if exist C:\Job\Resume.txt (
   echo INPUT FILE C:\Job\Resume.txt
   echo OUTPUT FILE %JOB_RESUME_LOC%\%FILE_DATETIME%_info.dat
   copy C:\Job\Resume.txt%JOB_RESUME_LOC%\%FILE_DATETIME%_info.dat
   echo File Resume.txt copied
) else (
   echo File Resume.txt not copied
) 

Basically, If the file "Resume.txt" is 0KB, exit the program. Otherwise, continue running the program. I hope this was not too confusing!

Upvotes: 2

Views: 65

Answers (2)

Compo
Compo

Reputation: 38604

First line of batch script.

@Where/T C:\Job:Resume.txt 2>Nul|Find /V " 0 ">Nul||Exit/B 1

This will exit the entire script if the file doesn't exist or has a size of zero. Then you just need to put your normal script underneath that line.

Upvotes: 0

JosefZ
JosefZ

Reputation: 30113

Based on Stephan's comment:

for %%a in ("C:\Job\Resume.txt") do set /A size=%%~za + 0
if %size% GTR 0 (
   echo INPUT FILE C:\Job\Resume.txt
   echo OUTPUT FILE %JOB_RESUME_LOC%\%FILE_DATETIME%_info.dat
   copy "C:\Job\Resume.txt" %JOB_RESUME_LOC%\%FILE_DATETIME%_info.dat
   echo File Resume.txt copied
) else (
   echo File Resume.txt not copied
)

For explanation, read call /? and Command Line arguments (Parameters). Next quoted statement is applied to %a parameter:

%~za expands %a to size of file

If "C:\Job\Resume.txt" file does not exists, then %~za expands to an empty string.
Hence, set /A size=%%~za + 0 returns 0.

Upvotes: 1

Related Questions