Change the value of errorlevel for testing purposes

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
sacha99
Posts: 1
Joined: 23 Jul 2019 08:05

Change the value of errorlevel for testing purposes

#1 Post by sacha99 » 23 Jul 2019 08:18

Hello,
I need to change a line in some old scripts from this

Code: Select all

If Not ERRORLEVEL 0 (
to this

Code: Select all

IF %errorlevel% NEQ 0
Because of this change I need to test the scripts to make sure it doesn't change the output of the scripts. How can I set the errorlevel ? From my search on internet, it seems I cannot change it.

Thank you for your help.

aGerman
Expert
Posts: 3761
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Change the value of errorlevel for testing purposes

#2 Post by aGerman » 23 Jul 2019 13:08

Easiest way to set the errorlevel to a certain value:

Code: Select all

cmd /c exit 5
echo %errorlevel%
Steffen

dbenham
Expert
Posts: 2288
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Change the value of errorlevel for testing purposes

#3 Post by dbenham » 23 Jul 2019 13:47

sacha99 wrote:
23 Jul 2019 08:18
I need to change a line in some old scripts from this

Code: Select all

If Not ERRORLEVEL 0 (
to this

Code: Select all

IF %errorlevel% NEQ 0
In case you don't know, those two statements give entirely different results.

IF NOT ERRORLEVEL 0 only returns TRUE if ERRORLEVEL is less than 0. It returns FALSE if greater than or equal to 0.

IF %ERRORLEVEL% NEQ 0 returns TRUE if ERRORLEVEL is greater than 0 or less than 0. It only returns FALSE if exactly 0.


Dave Benham

Post Reply