简体   繁体   English

如何在批处理中检查文件是否为空

[英]How to check if a file is not empty in Batch

There are several ways google throws at me for checking if a file is empty but I need to do the opposite. google有几种方法可以检查文件是否为空,但我需要做相反的事情。

If (file is NOT empty)

do things

How would I do this in batch? 我怎么批量做?

for /f %%i in ("file.txt") do set size=%%~zi
if %size% gtr 0 echo Not empty

this should work: 这应该工作:

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

help if says that you can add the NOT directly after the if to invert the compare statement help if说你可以在if之后直接添加NOT来反转compare语句的帮助

set "filter=*.txt"
for %%A in (%filter%) do if %%~zA==0 echo."%%A" is empty

Type help for in a command line to have explanations about the ~zA part 在命令行中键入help for以获得有关~zA部分的说明

You can leverage subroutines/external batch files to get to useful parameter modifiers which solve this exact problem 您可以利用子例程/外部批处理文件来获取有用的参数修饰符 ,从而解决这个问题

@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)

Alternatively 另外

notEmpty.bat notEmpty.bat

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

yourScript.bat yourScript.bat

Call notEmpty.bat file.txt
If %errorlevel% EQU 0 (
    Echo the file is not empty
) Else (
    Echo the file is empty
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM