Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows BAT : test if a specific file is empty

I would like to check if a specific file is empty in a windows .bat file. Here is my non working script :

set dir="C:\test"
set file="%dir%\fff.txt"

cd %dir%
if %file%%~zi == 0 exit
ftp -s:"%dir%\ftp.action"
exit

Could you help me debug this please ?

like image 639
Réjôme Avatar asked Mar 30 '11 08:03

Réjôme


2 Answers

Or try it with

@echo off
set "dir=C:\temp"
set "file=%dir%\a.txt"

call :CheckEmpty "%file%"
goto :eof

:CheckEmpty
if %~z1 == 0 exit
ftp -s:"%dir%\ftp.action"
goto :eof

The main difference is that I use a function call and use the %~z1, as the modifiers only works for paramters like %1, %2..%9 or for-loop parameters like %%a ...

like image 172
jeb Avatar answered Sep 21 '22 16:09

jeb


batch solution using file compare:

type nul > blank
fc myfile blank > nul
if errorlevel 1 echo myfile is not empty
like image 39
BearCode Avatar answered Sep 18 '22 16:09

BearCode