How to know what errorlevels a console applikation sets


J

jeeji

Hi

I am relatively new at creating command scripts. I am having troubles
with the errorlevel, and checking whether it contains an erroneous
value (I know it depends on the application called before it)

For example I found that calling
'taskkill' sets the errorlevel to 128
but calling 'rmdir' does not set it at all.

That means that if I have the following command script:

taskkill /IM app.exe
rmdir C:\Temp
if errorlevel 1 echo There is an error

The above echoes "There is an error", even though rmdir does not seem
to set the errorlevel.

I have two questions:
- How do I know which errorlevels are set by which applications
- And what is best practice for the above. Should I know that rmdir
does not set it, and therefore reset it to 0 after the call to
'taskkill'?

Thanks a lot

Jeeji
 
Ad

Advertisements

B

billious

jeeji said:
Hi

I am relatively new at creating command scripts. I am having troubles
with the errorlevel, and checking whether it contains an erroneous
value (I know it depends on the application called before it)

For example I found that calling
'taskkill' sets the errorlevel to 128
but calling 'rmdir' does not set it at all.

That means that if I have the following command script:

taskkill /IM app.exe
rmdir C:\Temp
if errorlevel 1 echo There is an error

The above echoes "There is an error", even though rmdir does not seem
to set the errorlevel.

I have two questions:
- How do I know which errorlevels are set by which applications
- And what is best practice for the above. Should I know that rmdir
does not set it, and therefore reset it to 0 after the call to
'taskkill'?

Thanks a lot

Jeeji

Somewhat a matter of experience. Some "inbuilt" commands set the errorlevel
because they run an executable (like FIND.EXE) whereas others inbuilt into
CMD.EXE and don't change the ERRORLEVEL.

I'd look at alt.msdos.batch.nt for NT-and-later batch methods
(alt.msdos.batch for DOS/9x)

As for your problem - it depends on quite what you are trying to achieve. Do
you want to detect whether the directory existed or whether it was deleted
or whether the attempt-to-delete operation failed?

Try this demo:


----- batch begins -------
[1]@echo off
[2]if not exist .\nothere\* echo doesn't exist
[3]md nothere
[4]cd nothere
[5]if not exist ..\nothere\* echo still doesn't exist
[6]if exist ..\nothere\* echo it's here
[7]rd ..\nothere 2>nul
[8]if exist ..\nothere\* echo it's STILL here - but so am I
[9]if not exist ..\nothere\* echo it's gone so where am I?
[10]cd ..
[11]rd .\nothere >nul
[12]if exist .\nothere\* echo it's STILL here
[13]if not exist .\nothere\* echo it's gone now
[14]
------ batch ends --------
Note the "2>nul" on line 7 which suppresses the error message generated
(since the processs will have a current directory of ".\nothere" and hence
".\nothere" can't be deleted)

There are many methods to set ERRORLEVEL to zero if that is what you want.

I use

echo x|find "x" >nul

which is easily editable to set errorlevel 1 so it's quick for testing....
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top