2012-06-27 16 views

Respuesta

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

regresa: >> inesperado 0 – Tertium

+0

@Tertium - La solución es agregar 'set size = 0' en la línea antes del bucle' for'. –

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

Tipo help for en una línea de comandos para tener explicaciones sobre la parte ~ zA

7

esto debería funcionar:

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

help if dice que se puede añadir el NOT directamente después de la if para invertir la declaración de comparación

+0

no debería haber un Switch después si if: para %% R in (test.dat) do si/i no %% ~ zR EQU 0 echo no está vacío – Christian4145

2

puede aprovechar subrutinas/archivos por lotes externos para llegar a parameter modifiers útil que resuelven este problema exacto

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

Alternativamente

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 
) 
Cuestiones relacionadas