setting ERRORLEVEL to 0

14

7

I have a batch file that is a wrapper around an installer. This batch file checks the error level returned by the installer and prints accordingly.

I noticed that if I execute set ERRORLEVEL=0 in a command prompt right before kicking off the batch file (in the same command window/environment), the installer never messes with the errorlevel and my batch script always returns passed. I would assume %ERRORLEVEL% is a variable defined by Windows and is used specifically to print out errors from programs and scripts and that using the variable in a batch file or something else would be 'at your own risk' because it could be changed at any moment by another process.

From what it seems like, when I set errorlevel in the given environment, it then somehow terminates the use of errorlevel as a holder of the exit code. Does anyone know why this is? To me its just weird unexpected behavior. Any information on the subject would be greatly appreciated!

user972276

Posted 2013-09-23T18:42:22.930

Reputation: 637

See also  Command script exit code not seen by && or ||.

– Scott – 2020-02-19T01:18:21.287

Answers

14

You are redefining the system variable into a regular one. When you do this, the system will not use it until the command session is closed.

The best way would be to use

exit /b 0

in another batch file and call it from your primary script. Where the number 0 is your wanted errorlevel. Either that or use a command that resets the errorlevel for you, such as echo, findstr etc.

For example the following commands would all set ERRORLEVEL to 0 within your batch-file:

VERIFY > nul

cmd /c "exit /b 0"

ver > nul     

TwirlMandarin

Posted 2013-09-23T18:42:22.930

Reputation: 1 510

1"Either that or use a command that resets the errorlevel for you, such as echo, findstr etc."

The echo command does not reset the errorlevel variable. – antred – 2015-05-11T14:33:42.777

As others have said, if the errorlevel *variable* has been "manually" set by the user/batch with "set errorlevel=0", the errorlevel variable will no longer reflect the exit code, but you can still access the exit code with if errorlevel do something. – Kevin Fegan – 2016-07-09T07:32:32.023

(1) A more general question is “How do I set ERRORLEVEL to some arbitrary value, such as 42?”  cmd /c exit 42 seems to be the best answer to that. (2) As dbenham points out, you don’t need the /b or the quotes.

– Scott – 2020-02-19T01:18:24.647

2Almost correct. You can get the "system" to use the dynamic ERRORLEVEL simply by undefining any user defined value using set "errorlevel=" – dbenham – 2013-09-23T19:41:09.060

Why does Windows allow someone to redefine a system variable into a regular one? Do both definitions still exist, its just echo and other such commands now point to the regular one? So programs could still be updating the system variable, its just not accessible through the command prompt? – user972276 – 2013-09-23T19:55:23.357

12

You should never assign your own value to dynamic system variables like ERRORLEVEL, PATH, CD, DATE, TIME, etc. Doing so will prevent code from seeing the dynamic value. You can restore the dynamic value by simply undefining the user defined value. For example:

set "errorlevel="

If you want to force the errorlevel to 0, then you can use this totally non-intuitive, but very effective syntax: (call ). The space after call is critical. If you want to set the errorlevel to 1, you can use (call). It is critical that there not be any space after call.

(call)
echo %errorlevel%
(call )
echo %errorlevel%

A more intuitive, but less convenient method to set the errorlevel is to use a dedicated subroutine in a batch file:

call :setErr 1
echo %errorlevel%
call :setErr 0
echo %errorlevel%
exit /b

:setErr
exit /b %1

Or if on the command line, you could use

cmd /c exit 1
echo %errorlevel%
cmd /c exit 0
echo %errorlevel%

dbenham

Posted 2013-09-23T18:42:22.930

Reputation: 9 212

7

I personally use this:

cd .

Works even in unix shell.

Andry

Posted 2013-09-23T18:42:22.930

Reputation: 71

But, this one might be a bit faster: type nul>nul Because Process Monitor shows QueryDirectory calls on cd . PS: cd . has another nice side effect in the unix shell. It does restore recreated working directory in the terminal if it has been opened before the erase. – Andry – 2020-02-28T03:50:47.037

2

Here are some other ways to reset the ErrorLevel state, which even work in MS-DOS (at least for version 6.22):

more < nul > nul

rem // The `> nul` part can be omitted in Windows but is needed in MS-DOS to avoid a line-break to be returned:
sort < nul > nul

The following methods work in MS-DOS only:

command /? > nul

fc nul nul > nul

For the sake of completeness, this sets the ErrorLevel state to 1, valid for both Windows and MS-DOS:

< nul find ""

aschipfl

Posted 2013-09-23T18:42:22.930

Reputation: 553

1

This is the only thing I have found that works.

call (exit /b 0)

Needed it in a script which uses 3rd party script that detects ERRORLEVEL, but is not resetting error level its self properly.

Richard Meadows

Posted 2013-09-23T18:42:22.930

Reputation: 11

0

These will also work: (Sets errorlevel to 1)

Method 1

color 00

Method 2

echo A | find "B"

You can also use this to set errorlevel 1-26:

echo Your errorlevel<sup>th</sup> character between A-Z | choice /c:Enter A-Z here /n

Wasif Hasan

Posted 2013-09-23T18:42:22.930

Reputation: 827