Solved

DOS batch errorlevel

Posted on 2001-06-21
12
1,313 Views
Last Modified: 2013-12-28
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.
0
Comment
Question by:txholdem
  • 5
  • 4
  • 3
12 Comments
 
LVL 7

Expert Comment

by:Ajnin
Comment Utility
What do you mean by empty file? A text file without any text?
Please explain
0
 
LVL 1

Author Comment

by:txholdem
Comment Utility
yes. a file that has nothing in it. its size is zero byte.
0
 
LVL 7

Expert Comment

by:Ajnin
Comment Utility
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
 
LVL 11

Expert Comment

by:griessh
Comment Utility
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
 
LVL 11

Expert Comment

by:griessh
Comment Utility
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
 
LVL 11

Accepted Solution

by:
griessh earned 30 total points
Comment Utility
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 7

Expert Comment

by:Ajnin
Comment Utility
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
 
LVL 11

Expert Comment

by:griessh
Comment Utility
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
 
LVL 1

Author Comment

by:txholdem
Comment Utility
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
 
LVL 1

Author Comment

by:txholdem
Comment Utility
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
 
LVL 7

Expert Comment

by:Ajnin
Comment Utility
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
 
LVL 11

Expert Comment

by:griessh
Comment Utility
>>'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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

The use of stolen credentials is a hot commodity this year allowing threat actors to move laterally within the network in order to avoid breach detection.
Today, still in the boom of Apple, PC's and products, nearly 50% of the computer users use Windows as graphical operating systems. If you are among those users who love windows, but are grappling to keep the system's hard drive optimized, then you s…
In this video, we discuss why the need for additional vertical screen space has become more important in recent years, namely, due to the transition in the marketplace of 4x3 computer screens to 16x9 and 16x10 screens (so-called widescreen format). …
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now