Réjôme
Réjôme

Reputation: 1484

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 ?

Upvotes: 9

Views: 31511

Answers (4)

Kumāra Bhikkhu
Kumāra Bhikkhu

Reputation: 101

for /F %%A in ("myfile") do If %%~zA equ 0 echo myfile is empty

Upvotes: 0

BearCode
BearCode

Reputation: 2950

batch solution using file compare:

type nul > blank
fc myfile blank > nul
if errorlevel 1 echo myfile is not empty

Upvotes: 5

jeb
jeb

Reputation: 82267

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 ...

Upvotes: 8

Mark Mooibroek
Mark Mooibroek

Reputation: 7696

Try this:

    Const ForReading = 1

    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFile = objFSO.OpenTextFile("c:\boot.ini", ForReading)


    Dim arrFileLines()
    i = 0
    Do Until objFile.AtEndOfStream
      Redim Preserve arrFileLines(i)
      arrFileLines(i) = objFile.ReadLine
      i = i + 1
    Loop
    objFile.Close

Upvotes: -1

Related Questions