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?
for /f %%i in ("file.txt") do set size=%%~zi
if %size% gtr 0 echo Not empty
set size=0 on the line before the for loop.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
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
)
empty rather than notEmpty, though.