I want to have a batch file which checks what the filesize
is of a file.
If it is bigger than %somany% kbytes,
it should redirect with GOTO to somewhere else.
Example:
[check for filesize]
IF %file% [filesize thing Bigger than] GOTO No
echo Great! Your filesize is smaller than %somany% kbytes.
pause
exit
:no
echo Um... You have a big filesize.
pause
exit
I like @Anders answer because the explanation of the %~z1 secret sauce. However, as pointed out, that only works when the filename is passed as the first parameter to the batch file.
@Anders worked around this by using
FOR
, which, is a great 1-liner fix to the problem, but, it's somewhat harder to read.Instead, we can go back to a simpler answer with %~z1 by using
CALL
. If you have a filename stored in an environment variable it will become %1 if you use it as a parameter to a routine in your batch file:As usual, VBScript is available for you to use.....
Save as
filesize.vbs
and enter on the command-line:Use a
for
loop (in batch) to get the return result.%~z1
expands to the size of the first argument to the batch file. Seeand
Simple example:
Create a one line batch file GetFileSize.bat containing
then call it
You can even create your test file on the fly to eliminate the pesky required file, note the double percent in the echo statement:
This latter solution is antispaghetti. So nice. However, more disk writes. Check use count.
Important to note is the INT32 limit of Batch: 'Invalid number. Numbers are limited to 32-bits of precision.'
Try the following statements:
Any number greater than the max INT32 value will BREAK THE SCRIPT! Seeing as filesize is measured in bytes, the scripts will support a maximum filesize of about 255.9999997615814 MB !
I prefer to use a DOS function. Feels cleaner to me.