Solved

DOS batch errorlevel

Posted on 2001-06-21
12
1,319 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 3
12 Comments
 
LVL 7

Expert Comment

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

Author Comment

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

Expert Comment

by:Ajnin
ID: 6214468
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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
LVL 11

Expert Comment

by:griessh
ID: 6216618
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
ID: 6216682
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
ID: 6216730
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
 
LVL 7

Expert Comment

by:Ajnin
ID: 6218083
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
ID: 6218124
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
ID: 6218936
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
ID: 6218958
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
ID: 6219022
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
ID: 6219178
>>'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

Flexible connectivity for any environment

The KE6900 series can extend and deploy computers with high definition displays across multiple stations in a variety of applications that suit any environment. Expand computer use to stations across multiple rooms with dynamic access.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
When asking a question in a forum or creating documentation, screenshots are vital tools that can convey a lot more information and save you and your reader a lot of time
This video Micro Tutorial explains how to clone a hard drive using a commercial software product for Windows systems called Casper from Future Systems Solutions (FSS). Cloning makes an exact, complete copy of one hard disk drive (HDD) onto another d…
Windows 8 comes with a dramatically different user interface known as Metro. Notably missing from the new interface is a Start button and Start Menu. Many users do not like it, much preferring the interface of earlier versions — Windows 7, Windows X…

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