Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Resetting ERRORLEVEL in batch scripts

1,409 views
Skip to first unread message

Duncan Mackinder

unread,
Jul 14, 1998, 3:00:00 AM7/14/98
to
I'm having trouble with ERRORLEVEL in batch scripts. In a DOS box, once an
error has occured ERRORLEVEL appears to remain set across calls to batch
scripts. I can't find a way to clear the ERRORLEVEL setting. I have done
SET ERRORLEVEL=
to ensure I don't have any environment variable called ERRORLEVEL.

I'm using NT 4.0 Workstation with service pack 3 incase this is relevant.

Any help or hints would be appreciated.

Duncan Mackinder

Frederic

unread,
Jul 14, 1998, 3:00:00 AM7/14/98
to

Duncan Mackinder schrieb:

This is normal. It's an internal variable. I don't know any method
to reset it to zero. You could try to write a program that terminates
with errorlevel 0.
But that should not be necessary. Why do you ask for the errorlevel
value twice? You can ask for it thousands of times in your batch, but
always preceded by a program that returns a termination code.
It seems somewhat weird to me that the fact that the value remains
unchanged disturbs the execution of your batch...


Duncan Mackinder

unread,
Jul 14, 1998, 3:00:00 AM7/14/98
to
Frederic

If I write a script as follows:

@echo off
if ERRORLEVEL 1 goto err
echo I'm OK
goto :eof

:err
echo Failed

Then I run this it prints
I'm OK

Next I type:
junk
which fails
after which whenever I run the script it prints
Failed.

It occurs to me as I write this that maybe all the commands such
as echo, set, if, call (my real script is obviously more complex than
the above) are maybe not treated the same as running a program
and thus don't set the ERRORLEVEL.

I'd assumed that things like echo, if and call would reset the ERRORLEVEL
since they succeed. I'll try your suggestion of calling some small program
which sets error code to 0 and see if that works.

Steve Hardy in another newsgroup to which I also posted my question
suggests:
ver >nul

I'll try that.

Thanks for your help.

Duncan

Frederic wrote in message <35AB9DA7...@primus-online.de>...

Allison

unread,
Jul 15, 1998, 3:00:00 AM7/15/98
to
Tp "reset" the ERRORLEVEL, simply run any EXE that returns 0. If you don't
have one, you could write one easily as a generic "do nothing" program that
always returns 0 upon termination.
Allison


Duncan Mackinder <mac...@cai.com> wrote in article
<6og3en$2rg17@usilws15>...

Ted Davis

unread,
Jul 15, 1998, 3:00:00 AM7/15/98
to
On Tue, 14 Jul 1998 21:17:27 +0100, "Duncan Mackinder"
<dun...@mackinder.demon.co.uk> wrote:

>Frederic
>
>If I write a script as follows:
>
>@echo off
>if ERRORLEVEL 1 goto err
>echo I'm OK
>goto :eof
>
>:err
>echo Failed
>
>Then I run this it prints
>I'm OK
>
>Next I type:
> junk
>which fails
>after which whenever I run the script it prints
>Failed.
>
>It occurs to me as I write this that maybe all the commands such
>as echo, set, if, call (my real script is obviously more complex than
>the above) are maybe not treated the same as running a program
>and thus don't set the ERRORLEVEL.

You have a *serious* misunderstanding of what ERRORLEVEL is - it's the
return value from an EXE or COM program that terminates with INT 21,
function 4C. Any other means of termination returns a zero. Internal
commands (those that are part of the command interpreter rather than
external files) *cannot* change ERRORLEVEL because they don't
terminate in the usual sense - they are in the nature of subroutines
inside the command processor that is interpreting the script.

>
>I'd assumed that things like echo, if and call would reset the ERRORLEVEL
>since they succeed. I'll try your suggestion of calling some small program
>which sets error code to 0 and see if that works.
>
>Steve Hardy in another newsgroup to which I also posted my question
>suggests:
> ver >nul
>

Probably any external command or program (anything implemented as a
.EXE or .COM file) should work. You could use DEBUG to create a
trivial program

DEBUG clearel.com
A
int 20

rcx
2
w
q

(INT 20 is reputed to resolve to INT 21, function 4c with a return
code of zero.)

T.E.D. (tda...@gearbox.maem.umr.edu)
SPAM filter: Messages to this address *must* contain "T.E.D."
somewhere in the body or they will be automatically rejected.

Tim Kannel

unread,
Jul 15, 1998, 3:00:00 AM7/15/98
to
<snip>

> It occurs to me as I write this that maybe all the commands such
> as echo, set, if, call (my real script is obviously more complex than
> the above) are maybe not treated the same as running a program
> and thus don't set the ERRORLEVEL.
<snip>

The errorlevel is a return value from EXTERNAL programs. INTERNAL
commands (such as echo and set) do not return values. Although there
would be some use for errorlevel as a means of obtaining an error code
from the last operating system command it is generally unncessary
(e.g., what use would an error code for "echo" be?).

As previously pointed out, it is also unusual to use errorlevel
without explicitly running a program right before it.

However, it can be set to zero with a very simple program.
One way to create such a program is by using a DEBUG:

debug < debug.scr

where debug.scr (.scr is for script) is as follows (to produce a program
named "CERROR.COM", for clear-errorlevel):

--DEBUG.SCR - cut here----
n cerror.com
a
mov ax,4c00
int 21

r bx
0
r cx
5
w
q
--DEBUG.SCR - cut here---

Note that the blank line after the "int 21" is necessary.

--
Tim Kannel
Computer Science junior, U of M - Twin Cities
FTPDIF 2.1 - Finds differences in FTP directory listings
ftp://ftp.simtel.net/pub/simtelnet/msdos/00_info/ftpdif21.zip
TCAP 3.0 - Captures console I/O to a file
ftp://ftp.simtel.net/pub/simtelnet/msdos/sysutl/tcap30.zip

0 new messages