bat goto:label if a file is empty

January 22, 2009 at 17:10:32
Specs: Windows XP, Athlon x2 5200+ / 2G
Hello, I need to make a batch script like
ls *.rtf *.doc > text.txt
if (text.txt is empty file) then goto label
other commands
del text.txt

I don't know how to make the if, test file
size, and goto combination


See More: bat goto:label if a file is empty

Report •

January 22, 2009 at 19:41:42

Here's a solution I found over in the DOS forum using the File Compare (FC) command. First, you create a known empty file and then compare it to text.txt. If they're the same, text.txt is empty.

Report •

January 22, 2009 at 23:43:46
Gotta ask - what is 'ls'

Report •

January 23, 2009 at 00:14:18
Try this. Enter For/? at the command prompt to view For command Help.

:: Code begins.....

@echo off

ls *.rtf *.doc > text.txt
for /f %%A in ('dir /b test.txt') do (
    if %%~zA lss 1 goto label

:: other commands here...

echo Filesize less than 1
del text.txt

:: Code ends.....

Good luck

Report •

Related Solutions

January 23, 2009 at 02:30:47
There's no ls in DOS; nor in XP.

So first get clear about what OS you're using.

In DOS there is no for /f.

In DOS you can use FC:

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

If at first you don't succeed, you're about average.


Report •

January 23, 2009 at 06:11:02
ls is the UNIX equivalent to dir

Report •

January 23, 2009 at 14:50:18
Don't I see that bearc lists his OS as Windows XP?

Report •

Ask Question