我喜欢@Anders 的回答,因为 %~z1 秘诀的解释。但是,正如所指出的,仅当文件名作为第一个参数传递给批处理文件时才有效。
@Anders 通过 usingFOR
解决了这个问题,这是解决问题的一个很好的 1-liner 解决方案,但是,它有点难以阅读。
相反,我们可以使用 %~z1 回到更简单的答案CALL
。如果您将文件名存储在环境变量中,如果您将其用作批处理文件中例程的参数,它将变为 %1:
@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
我一直对 J. Bouvrie 对 32 位限制的担忧感到好奇。看来他正在谈论LSS
在文件大小逻辑本身上使用 not 的问题。为了解决 J. Bouvrie 的问题,我重写了解决方案以使用填充字符串比较:
@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