I want to have a batch file which checks what the filesize
is of a file.
我想要一个批处理文件,它检查文件的filesize是什么。
If it is bigger than %somany% kbytes,
it should redirect with GOTO to somewhere else.
如果它大于%somany% kbytes,它应该将GOTO重定向到其他地方。
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
13 个解决方案
#1
83
If the file name is used as a parameter to the batch file, all you need is %~z1
(1 means first parameter)
如果将文件名用作批处理文件的参数,那么您所需要的就是%~z1(1表示第一个参数)
If the file name is not a parameter, you can do something like:
如果文件名不是参数,您可以执行以下操作:
@echo off
setlocal
set file="test.cmd"
set maxbytesize=1000
FOR /F "usebackq" %%A IN ('%file%') DO set size=%%~zA
if %size% LSS %maxbytesize% (
echo.File is ^< %maxbytesize% bytes
) ELSE (
echo.File is ^>= %maxbytesize% bytes
)
#2
10
%~z1
expands to the size of the first argument to the batch file. See
%~z1扩展到批处理文件的第一个参数的大小。看到
C:\> call /?
and
和
C:\> if /?
Simple example:
简单的例子:
@ECHO OFF
SET SIZELIMIT=1000
SET FILESIZE=%~z1
IF %FILESIZE% GTR %SIZELIMIT% Goto No
ECHO Great! Your filesize is smaller than %SIZELIMIT% kbytes.
PAUSE
GOTO :EOF
:No
ECHO Um ... You have a big filesize.
PAUSE
GOTO :EOF
#3
9
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答案,因为对%~z1秘制酱的解释。但是,正如指出的那样,只有当文件名作为批处理文件的第一个参数传递时才有效。
@Anders worked around this by using FOR
, which, is a great 1-liner fix to the problem, but, it's somewhat harder to read.
@Anders通过使用FOR来解决这个问题,这是解决这个问题的一个很好的方法,但是,阅读起来有点困难。
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:
相反,我们可以通过调用来返回一个简单的%~z1的答案。如果您在一个环境变量中存储了一个文件名,那么在批处理文件中,如果您将它用作一个参数,那么它将成为%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 %maxbytesize% bytes
)
goto :eof
:setsize
set size=%~z1
goto :eof
#4
8
If your %file%
is an input parameter, you may use %~zN
, where N
is the number of the parameter.
如果您的%file%是一个输入参数,您可以使用%~zN,其中N是参数的数目。
E.g. a test.bat
containing
例如,一个测试。蝙蝠包含
@echo %~z1
Will display the size of the first parameter, so if you use "test myFile.txt
" it will display the size of the corresponding file.
将显示第一个参数的大小,因此如果您使用“测试myFile”。它将显示相应文件的大小。
#5
7
I prefer to use a DOS function. Feels cleaner to me.
我喜欢使用DOS功能。我觉得干净。
SET SIZELIMIT=1000
CALL :FileSize %1 FileSize
IF %FileSize% GTR %SIZELIMIT% Echo Large file
GOTO :EOF
:FileSize
SET %~2=%~z1
GOTO :EOF
#6
4
As usual, VBScript is available for you to use.....
像往常一样,VBScript可以供您使用。
Set objFS = CreateObject("Scripting.FileSystemObject")
Set wshArgs = WScript.Arguments
strFile = wshArgs(0)
WScript.Echo objFS.GetFile(strFile).Size & " bytes"
Save as filesize.vbs
and enter on the command-line:
另存为文件大小。vbs并输入命令行:
C:\test>cscript /nologo filesize.vbs file.txt
79 bytes
Use a for
loop (in batch) to get the return result.
使用for循环(批量)获取返回结果。
#7
4
Another example
另一个例子
FOR %I in (file1.txt) do @ECHO %~zI
#8
2
Create a one line batch file GetFileSize.bat containing
创建一个行批文件GetFileSize。蝙蝠包含
GetFileSize=%~z1
then call it
然后调用它
call GetFileSize myfile.txt
if (%GetFileSize) == () goto No_File
if (%GetFileSize) == (0) goto No_Data
if (%GetFileSize) GTR 1000 goto Too_Much_Data
rem Etc.
You can even create your test file on the fly to eliminate the pesky required file, note the double percent in the echo statement:
您甚至可以在苍蝇上创建您的测试文件,以消除讨厌的要求文件,请注意echo语句中的双重百分比:
echo set GetFileSize=%%~z1 > %temp%\GetFileSize.bat
call %temp%\GetFileSize myfile.txt
if (%GetFileSize) GTR 1000 goto Too_Much_Data
rem etc
This latter solution is antispaghetti. So nice. However, more disk writes. Check use count.
后一种解决方案是反意大利面。太好了。然而,更多的磁盘写。检查使用计数。
#9
1
Just saw this old question looking to see if Windows had something built in. The ~z thing is something I didn't know about, but not applicable for me. I ended up with a Perl one-liner:
刚刚看到这个老问题,想看看窗户是否有内置的东西。这个~z的东西我不知道,但不适合我。最后,我使用了一个Perl一行程序:
@echo off
set yourfile=output.txt
set maxsize=10000
perl -e "-s $ENV{yourfile} > $ENV{maxsize} ? exit 1 : exit 0"
rem if %errorlevel%. equ 1. goto abort
if errorlevel 1 goto abort
echo OK!
exit /b 0
:abort
echo Bad!
exit /b 1
#10
1
This was my solution for evaluating file sizes without using VB/perl/etc. and sticking with native windows shell commands:
这是我在不使用VB/perl/等的情况下评估文件大小的方法。并坚持使用本地windows shell命令:
FOR /F "tokens=4 delims= " %%i in ('dir /-C %temp% ^| find /i "filename.txt"') do (
IF %%i GTR 1000000 (
echo filename.txt filesize is greater than 1000000
) ELSE (
echo filename.txt filesize is less than 1000000
)
)
Not the cleanest solution, but it gets the job done.
不是最干净的解决方案,但它能完成任务。
#11
0
After a few "try and test" iterations I've found a way (still not present here) to get size of file in cycle variable (not a command line parameter):
经过几次“尝试和测试”的迭代后,我发现了一个方法(仍然不在这里)以在循环变量中获取文件的大小(而不是命令行参数):
for %%i in (*.txt) do (
echo %%~z%i
)
#12
-1
Important to note is the INT32 limit of Batch: 'Invalid number. Numbers are limited to 32-bits of precision.'
需要注意的是批次的INT32限制:“无效数字”。数字被限制在32位精度。
Try the following statements:
试试下面的语句:
IF 2147483647 GTR 2147483646 echo A is greater than B (will be TRUE)
IF 2147483648 GTR 2147483647 echo A is greater than B (will be FALSE!)
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 !
任何大于最大INT32值的数字都会破坏脚本!以字节为单位来衡量文件的大小,脚本将支持大约255.9999997615814 MB的最大文件大小!
#13
-4
Just an idea:
只是一个想法:
You may get the filesize by running command "dir":
您可以通过运行命令“dir”来获得filesize:
>dir thing
Then again it returns so many things.
然后它又返回了很多东西。
Maybe you can get it from there if you look for it.
也许你可以从那里得到它。
But I am not sure.
但我不确定。
#1
83
If the file name is used as a parameter to the batch file, all you need is %~z1
(1 means first parameter)
如果将文件名用作批处理文件的参数,那么您所需要的就是%~z1(1表示第一个参数)
If the file name is not a parameter, you can do something like:
如果文件名不是参数,您可以执行以下操作:
@echo off
setlocal
set file="test.cmd"
set maxbytesize=1000
FOR /F "usebackq" %%A IN ('%file%') DO set size=%%~zA
if %size% LSS %maxbytesize% (
echo.File is ^< %maxbytesize% bytes
) ELSE (
echo.File is ^>= %maxbytesize% bytes
)
#2
10
%~z1
expands to the size of the first argument to the batch file. See
%~z1扩展到批处理文件的第一个参数的大小。看到
C:\> call /?
and
和
C:\> if /?
Simple example:
简单的例子:
@ECHO OFF
SET SIZELIMIT=1000
SET FILESIZE=%~z1
IF %FILESIZE% GTR %SIZELIMIT% Goto No
ECHO Great! Your filesize is smaller than %SIZELIMIT% kbytes.
PAUSE
GOTO :EOF
:No
ECHO Um ... You have a big filesize.
PAUSE
GOTO :EOF
#3
9
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答案,因为对%~z1秘制酱的解释。但是,正如指出的那样,只有当文件名作为批处理文件的第一个参数传递时才有效。
@Anders worked around this by using FOR
, which, is a great 1-liner fix to the problem, but, it's somewhat harder to read.
@Anders通过使用FOR来解决这个问题,这是解决这个问题的一个很好的方法,但是,阅读起来有点困难。
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:
相反,我们可以通过调用来返回一个简单的%~z1的答案。如果您在一个环境变量中存储了一个文件名,那么在批处理文件中,如果您将它用作一个参数,那么它将成为%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 %maxbytesize% bytes
)
goto :eof
:setsize
set size=%~z1
goto :eof
#4
8
If your %file%
is an input parameter, you may use %~zN
, where N
is the number of the parameter.
如果您的%file%是一个输入参数,您可以使用%~zN,其中N是参数的数目。
E.g. a test.bat
containing
例如,一个测试。蝙蝠包含
@echo %~z1
Will display the size of the first parameter, so if you use "test myFile.txt
" it will display the size of the corresponding file.
将显示第一个参数的大小,因此如果您使用“测试myFile”。它将显示相应文件的大小。
#5
7
I prefer to use a DOS function. Feels cleaner to me.
我喜欢使用DOS功能。我觉得干净。
SET SIZELIMIT=1000
CALL :FileSize %1 FileSize
IF %FileSize% GTR %SIZELIMIT% Echo Large file
GOTO :EOF
:FileSize
SET %~2=%~z1
GOTO :EOF
#6
4
As usual, VBScript is available for you to use.....
像往常一样,VBScript可以供您使用。
Set objFS = CreateObject("Scripting.FileSystemObject")
Set wshArgs = WScript.Arguments
strFile = wshArgs(0)
WScript.Echo objFS.GetFile(strFile).Size & " bytes"
Save as filesize.vbs
and enter on the command-line:
另存为文件大小。vbs并输入命令行:
C:\test>cscript /nologo filesize.vbs file.txt
79 bytes
Use a for
loop (in batch) to get the return result.
使用for循环(批量)获取返回结果。
#7
4
Another example
另一个例子
FOR %I in (file1.txt) do @ECHO %~zI
#8
2
Create a one line batch file GetFileSize.bat containing
创建一个行批文件GetFileSize。蝙蝠包含
GetFileSize=%~z1
then call it
然后调用它
call GetFileSize myfile.txt
if (%GetFileSize) == () goto No_File
if (%GetFileSize) == (0) goto No_Data
if (%GetFileSize) GTR 1000 goto Too_Much_Data
rem Etc.
You can even create your test file on the fly to eliminate the pesky required file, note the double percent in the echo statement:
您甚至可以在苍蝇上创建您的测试文件,以消除讨厌的要求文件,请注意echo语句中的双重百分比:
echo set GetFileSize=%%~z1 > %temp%\GetFileSize.bat
call %temp%\GetFileSize myfile.txt
if (%GetFileSize) GTR 1000 goto Too_Much_Data
rem etc
This latter solution is antispaghetti. So nice. However, more disk writes. Check use count.
后一种解决方案是反意大利面。太好了。然而,更多的磁盘写。检查使用计数。
#9
1
Just saw this old question looking to see if Windows had something built in. The ~z thing is something I didn't know about, but not applicable for me. I ended up with a Perl one-liner:
刚刚看到这个老问题,想看看窗户是否有内置的东西。这个~z的东西我不知道,但不适合我。最后,我使用了一个Perl一行程序:
@echo off
set yourfile=output.txt
set maxsize=10000
perl -e "-s $ENV{yourfile} > $ENV{maxsize} ? exit 1 : exit 0"
rem if %errorlevel%. equ 1. goto abort
if errorlevel 1 goto abort
echo OK!
exit /b 0
:abort
echo Bad!
exit /b 1
#10
1
This was my solution for evaluating file sizes without using VB/perl/etc. and sticking with native windows shell commands:
这是我在不使用VB/perl/等的情况下评估文件大小的方法。并坚持使用本地windows shell命令:
FOR /F "tokens=4 delims= " %%i in ('dir /-C %temp% ^| find /i "filename.txt"') do (
IF %%i GTR 1000000 (
echo filename.txt filesize is greater than 1000000
) ELSE (
echo filename.txt filesize is less than 1000000
)
)
Not the cleanest solution, but it gets the job done.
不是最干净的解决方案,但它能完成任务。
#11
0
After a few "try and test" iterations I've found a way (still not present here) to get size of file in cycle variable (not a command line parameter):
经过几次“尝试和测试”的迭代后,我发现了一个方法(仍然不在这里)以在循环变量中获取文件的大小(而不是命令行参数):
for %%i in (*.txt) do (
echo %%~z%i
)
#12
-1
Important to note is the INT32 limit of Batch: 'Invalid number. Numbers are limited to 32-bits of precision.'
需要注意的是批次的INT32限制:“无效数字”。数字被限制在32位精度。
Try the following statements:
试试下面的语句:
IF 2147483647 GTR 2147483646 echo A is greater than B (will be TRUE)
IF 2147483648 GTR 2147483647 echo A is greater than B (will be FALSE!)
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 !
任何大于最大INT32值的数字都会破坏脚本!以字节为单位来衡量文件的大小,脚本将支持大约255.9999997615814 MB的最大文件大小!
#13
-4
Just an idea:
只是一个想法:
You may get the filesize by running command "dir":
您可以通过运行命令“dir”来获得filesize:
>dir thing
Then again it returns so many things.
然后它又返回了很多东西。
Maybe you can get it from there if you look for it.
也许你可以从那里得到它。
But I am not sure.
但我不确定。