20

谷歌有几种方法可以检查文件是否为空,但我需要做相反的事情。

If (file is NOT empty)

do things

我将如何批量执行此操作?

4

4 回答 4

25
for /f %%i in ("file.txt") do set size=%%~zi
if %size% gtr 0 echo Not empty
于 2012-06-27T12:04:41.693 回答
8

这应该工作:

for %%R in (test.dat) do if not %%~zR lss 1 echo not empty

help if表示您可以NOT直接在之后添加if以反转比较语句

于 2012-06-27T12:04:08.310 回答
4
set "filter=*.txt"
for %%A in (%filter%) do if %%~zA==0 echo."%%A" is empty

键入help for命令行以获取有关 ~zA 部分的说明

于 2012-06-27T12:03:53.553 回答
4

您可以利用子例程/外部批处理文件来获得有用的参数修饰符来解决这个确切的问题

@Echo OFF
(Call :notEmpty file.txt && (
    Echo the file is not empty
)) || (
    Echo the file is empty
)
::exit script, you can `goto :eof` if you prefer that
Exit /B


::subroutine
:notEmpty
If %~z1 EQU 0 (Exit /B 1) Else (Exit /B 0)

或者

notEmpty.bat

@Echo OFF
If %~z1 EQU 0 (Exit /B 1) Else (Exit /B 0)

yourScript.bat

Call notEmpty.bat file.txt
If %errorlevel% EQU 0 (
    Echo the file is not empty
) Else (
    Echo the file is empty
)
于 2016-08-05T02:10:30.463 回答