Global exit on ERRORLEVEL 1?

K

Keith Hill

Is there a way to get a batch file to exit with a particular exit code (-1
in my case) when *any* command executed in the batch file sets ERRORLEVEL >=
1? It is kind of a pain to check the errorlevel after every command.
 
M

Matthias Tacke

Keith Hill said:
Is there a way to get a batch file to exit with a particular exit code
(-1 in my case) when *any* command executed in the batch file sets
ERRORLEVEL >= 1? It is kind of a pain to check the errorlevel after
every command.

Hello Kith,

AFAIK there is no command to allow a batch setting an errorlevel. But
since the last errorlevel stays when leaving the batch, you simple
issue a command which sets or resets the level. There is an example in
the help for setlocal (See my signature).

If you have a lot of commands to execute you may call a :sub which in-
cludes a general errorchecking setting a environment-var which is
checked on exit. But it is questionable why you should continue a batch
when the errorcondition is there.

hth
 
K

Keith Hill

Isn't it possible for each command to set the errorlevel? In this case, if
you want your script to exit with an error code whenever any error is
encountered (this is really what I am looking for), you have to test
errorlevel after every command that can return an error. This seems like a
hassle. KornShell scripts with "set -e" will exit with an error whenever
any error occurs without having to test every return code for an error.

--
Keith

Matthias Tacke said:
Hello Kith,

AFAIK there is no command to allow a batch setting an errorlevel. But
since the last errorlevel stays when leaving the batch, you simple
issue a command which sets or resets the level. There is an example in
the help for setlocal (See my signature).

If you have a lot of commands to execute you may call a :sub which in-
cludes a general errorchecking setting a environment-var which is
checked on exit. But it is questionable why you should continue a batch
when the errorcondition is there.
=
 
M

Matthias Tacke

Isn't it possible for each command to set the errorlevel? In this
case, if you want your script to exit with an error code whenever any
error is encountered (this is really what I am looking for), you have
to test errorlevel after every command that can return an error. This
seems like a hassle. KornShell scripts with "set -e" will exit with
an error whenever any error occurs without having to test every return
code for an error.
I misunderstood you then Keith.
It is possible for all external .exe programs and some internal to set
the errorlevel. In pure Dos the Choice.exe uses errorlevel to pass
entries to the batch.
MS OS are as different as the used shell is. I'm sorry, AFAIK such a
batchwide errorlevel doesn't exist. Look for a good editor and copy the
handler commands. You've to be strong and live with the pain :(
 
K

Keith Hill

Thanks for the reply. I can live with checking after every command. I just
wanted to make sure there wasn't another way to do this.
 
A

Austin M. Horst

Microsoft needs to add an "ON" program/command to the mix

ON ERRORLEVEL # GOTO :EOF
(Not a real command, yet)

Visual Basic has it

ON ERROR GOTO 0
ON ERROR RESUME NEXT

Austin M. Horst
 
A

Al Dunbar [MS-MVP]

There is one internal command that can set the errorlevel code, EXIT. Here
is the help text:

Quits the CMD.EXE program (command interpreter) or the current batch
script.

EXIT [/B] [exitCode]

/B specifies to exit the current batch script instead of
CMD.EXE. If executed from outside a batch script, it
will quit CMD.EXE

exitCode specifies a numeric number. if /B is specified, sets
ERRORLEVEL that number. If quitting CMD.EXE, sets the process
exit code with that number.

try creating a batch file seterror.cmd with this content:

@echo off
if "%1" EQU "" (
echo/Syntax: {call} %~n0 [errorlevelcode]
) else (
exit /b %1
)

You can then call this to set the errorlevel from within a batch script,
i.e.:

call seterror -1
call seterror 0
call seterror 123

A bit quirky, as an explicit set errorlevel={whatever} screws things up
somehow.

This does not fully fill the requirement, however, I suspect it may be of
some use.


/Al
 
K

Keith Hill

That is exactly the kind of construct I am looking for. Too bad it isn't
implemented.
 
M

Matthias Tacke

Al Dunbar said:
There is one internal command that can set the errorlevel code, EXIT.
Here is the help text:

Quits the CMD.EXE program (command interpreter) or the current batch
script.

EXIT [/B] [exitCode]
<snip>

Thanks Al,
learned something new (to me). I searched several times through the
help with the keyword "errorlevel" and didn't get exit. In w2k the
exit explanation doesn't include your hint. Only exit /? reveals this.
 
A

Al Dunbar [MS-MVP]

Matthias Tacke said:
Al Dunbar said:
There is one internal command that can set the errorlevel code, EXIT.
Here is the help text:

Quits the CMD.EXE program (command interpreter) or the current batch
script.

EXIT [/B] [exitCode]
<snip>

Thanks Al,
learned something new (to me). I searched several times through the
help with the keyword "errorlevel" and didn't get exit. In w2k the
exit explanation doesn't include your hint. Only exit /? reveals this.

I thought it was intuitive that a return code would be set by a command that
returns.

/Al
 

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