Reputation: 34627
There are several ways google throws at me for checking if a file is empty but I need to do the opposite.
If (file is NOT empty)
do things
How would I do this in batch?
Upvotes: 24
Views: 70106
Reputation: 13013
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
@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
)
Upvotes: 4
Reputation: 708
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
Upvotes: 9
Reputation: 31221
for /f %%i in ("file.txt") do set size=%%~zi
if %size% gtr 0 echo Not empty
Upvotes: 28
Reputation: 43489
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
Upvotes: 4