bat goto:label if a file is empty

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

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

thanks



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

Report •


#1
January 22, 2009 at 19:41:42
bearc,

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.

http://www.computing.net/answers/do...


Report •

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

Report •

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

:: Code begins.....

@echo off
cls

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

:: other commands here...

:label
echo Filesize less than 1
del text.txt
exit/b

:: Code ends.....

Good luck


Report •

Related Solutions

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

M2


Report •

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

Report •

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

Report •


Ask Question