I like @Anders answer because the explanation for the secret sauce is% ~ z1. However, as indicated, this only works when the file name is passed as the first parameter to the batch file.
@Anders circumvented this problem using FOR , which is a great solution to a 1 line problem, but harder to read.
Instead, we can return to a simpler answer with% ~ z1 using CALL . If you have a file name stored in an environment variable, it will become% 1 if you use it as a parameter for a routine in your batch file:
@echo off setlocal set file=test.cmd set maxbytesize=1000 call :setsize %file% if %size% lss %maxbytesize% ( echo File is less than %maxbytesize% bytes ) else ( echo File is greater than or equal %maxbytesize% bytes ) goto :eof :setsize set size=%~z1 goto :eof
I was curious about the concern of J. Bouvri regarding 32-bit restrictions. He seems to be talking about a problem using LSS not about the file size logic itself. To deal with J. Bouvrie's problem, I rewrote the solution to use comparison with the added string:
@echo on setlocal set file=test.cmd set maxbytesize=1000 call :setsize %file% set checksize=00000000000000000000%size% set checkmaxbytesize=00000000000000000000%maxbytesize% if "%checksize:~-20%" lss "%checkmaxbytesize:~-20%" ( echo File is less than %maxbytesize% bytes ) else ( echo File is greater than or equal %maxbytesize% bytes ) goto :eof :setsize set size=%~z1 goto :eof
Stephen Quan Apr 24 '15 at 11:43 2015-04-24 11:43
source share