DOS batch errorlevel

The following code, in a DOS batch file, works on NT. However, it does not work on Windows 98. It always prints "file is empty." How to make this work or how do I judge if a file is empty on Windows 98?

find /v "" file
if errorlevel=1 echo file is empty.

Thanks.
LVL 1
txholdemAsked:
Who is Participating?
 
griesshConnect With a Mentor Commented:
txholdem

You could use the copy command, that doesn't copy zero length files. Here is an example (very rough, but it works). I am sure from here on you can deal with it yourself, if not I'll give it another shot. Just let me know.

rem Try this with an empty file source1
rem ===================================
:step1
copy source1 temp > nul
if not exist temp goto empty1
echo source1 is not empty
del temp
goto step2

:empty1
echo file source1 is empty
goto step2


rem Try this with an non-empty file source2
rem =======================================
:step2
copy source2 temp > nul
if not exist temp goto empty2
echo source2 is not empty
del temp
goto end

:empty2
echo file source2 is empty
goto end

:end


It's a painful process, but it works down to MSDOS 5 at least.

Good Luck

=====
Werner
0
 
AjninCommented:
What do you mean by empty file? A text file without any text?
Please explain
0
 
txholdemAuthor Commented:
yes. a file that has nothing in it. its size is zero byte.
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
AjninCommented:
You can check by the size, but normally Windows98 doesn't keep files that are zero bytes in size.

Every now and then you may see it display a file that is zero bytes but you won't be able to delete it because windows can't actually see it. If you reboot the file will be gone.

I guess to answer your question Windows 98 doesn't keep empty files. I'll see if I can find a way to check via a batch file.
0
 
griesshCommented:
Ajnin, sorry, but I have to disagree.

Windows98 certainly keeps 0 byte files! And they won't get deleted! I guess you were looking into the ../temp directory when you noticed that.
I am using empty files as locks and they stay alive and can be deleted as every other file.

======
Werner
0
 
griesshCommented:
Ajnin, sorry, but I have to disagree.

Windows98 certainly keeps 0 byte files! And they won't get deleted! I guess you were looking into the ../temp directory when you noticed that.
I am using empty files as locks and they stay alive and can be deleted as every other file.

======
Werner
0
 
AjninCommented:
I still disagree. There is no way to put zero bytes of data on your HD. What would be put on the HD? The file has to have atleast one bit (If it was only one bit then windows will report it as 0KB since it rounds off.)

txholdem, I know your question is actually on the batch file itself. I'm still looking for a command that will let you know the size of the file. griessh's comment might be what you need though.

Let us know
Regards
0
 
griesshCommented:
Ajnin

Just try it, it is so easy. Create a file with no content. That doesn't mean it won't take up space, it just means there are 0 bytes in it. Do a "edit a", "File Save", "File Exit" on command prompt. You will see a file 'a' with 0 length and you can delete it and it will be there until you delete it.
The copy tricks is proven to work, I do that myself in smaller cleanup batch files.

======
Werner
0
 
txholdemAuthor Commented:
griessh,

That was tricky. I tried copying a 0-byte file and nothing was copied. So i guess your method should work. Even though I've done it by perl script and it's much easier! That takes care of all the differences between NT and 9x.

What I still don't understand though is if 'errorlevel' is a DOS key word, how come NT recognizes it while 9x does not?

0
 
txholdemAuthor Commented:
Ajnin, thanks for your help.

>There is no way to put zero bytes of data on your HD. What would be put on the HD?

why do think so? Doesn't the OS write the name of the file some where on the HD and a reference to its contents somewhere else? In this case, the referenced address contains nothing?
0
 
AjninCommented:
Well griessh you appear to be right about the zero byte file.

In my experience the only time I ever saw a 0 byte file was during a faulty download. I never actually thought that a ASCII file with no text was 0 bytes. I always thought that it had a bit or two on it.

Yes txholdem, you hit the nail on the head with the reason why it would be possible. It is the FAT (File Allocation Table) that stores info of the file and its location. "In this case, the referenced address contains nothing?" is exactly correct.
I just couldn't get the concept through my head until griessh suggested to try it and then I had to figure out why it was possible.
Always learning.

Glad your problem was fixed txholdem.
And thanks griessh.
Regards


0
 
griesshCommented:
>>'errorlevel' is a DOS key word, how come NT recognizes it while 9x does not?

I think it is not the errorlevel that fails, it is the find that behaves different. And admit 'find ""' is an unusual request. Doesn't every file contain an empty string? Apperently not the find in Win98.
I just wonder, doesn't relying on a failed 'copy' also just use a misbehaviour? A couple of UNIX features are certainly missing in Win.

Ajnin, you made me insecure. I had to sit down on a couple of different WinXX systems and try it myself :-)

======
Werner
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.