What is the easiest way to reset ERRORLEVEL to zero?
Solution 1
I found that "exit 0" looks like a good way to deal with this problem.
Usage Example:
NET STOP UnderDevService /Y
exit 0
if the UnderDevService service is not started.
Solution 2
if you use exit /b 0
you can return an errorlevel 0
from within a child batch script without also exiting the parent.
Solution 3
Seems to do the trick:
ver > nul
Not everything works, and it is not clear why. For example, the following do not:
echo. > nul
cls > nul
Solution 4
In a pre- or post-build event, if the return code of an executable is greater than zero, and the call to the executable is not the last line of the pre- or post-build event, a quick way to mute it and avoid triggering a check for a non-zero errorlevel
is to follow the failing line with a line that explicitly returns zero:
cmd /c "exit /b 0"
This is essentially a generic combination of the previously-mentioned solutions that will work with more than just the last line of a pre- or post-build event.
Solution 5
I personally use this:
cd .
Works even in unix shell.
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.
Update:
And that is a bit more faster:
call;
Related videos on Youtube
user95319
Updated on July 05, 2022Comments
-
user95319 almost 2 years
I have a post-build event that runs some commands for a c# project. The last command would sometimes cause the ERRORLEVEL value not equals to zero and then the build fails.
I want to append an extra line of command to always set the ERRORLEVEL value to zero. What is the most convenient way to do that?
-
Dykam almost 15 yearsThe build doesn't really fail, only the IDE does look like it.
-
Arun almost 11 yearsI realize this is a pretty old post... I had success in resetting the errorlevel to 0 by issuing the command "type nul" after the last command. Just felt it might be of use.
-
-
user95319 almost 15 yearsI think the reason of why "echo" and "cls" don't work is because they are the shell built-in commands, not real programs.
-
Jason Kresowaty almost 15 yearsI'll give you that, but where is ver.exe?
-
user95319 almost 15 yearsI can't find "ver.exe" or "ver.com" either. I don't know how to explain that.
-
madoki over 12 yearsnot if you also want to run that batch file from the command line, as exit 0 will close the window. cmd /c "exit /b 0" as suggested below is much more bening
-
madoki over 12 yearsthanks, this worked for me. the easier sounding suggestion of 'exit 0' above doesn't cut it as I wish to continue doing stuff after resetting the errorlevel, not exit
-
AnneTheAgile about 11 yearsAt batch command line, "ver" returns the MS windows version, eg "Microsoft Windows [Version 6.1.7601]".
-
Alyssa Haroldsen almost 11 yearseven more fancy: you can use
<some failing command> || cmd /c "exit /b 0"
as a one liner. -
ed9w2in6 about 10 yearsif you run help in command shell, you will see ver is also a built in command
-
jeb over 9 yearsIt's easy but it´s a really bad idea, as this creates a variable named
errorlevel
which overlays the internal pseudo variableerrorlevel
-
cirrus over 9 yearsCrikey, you're right! Wow, looks like I've been dodging bullets for years :)
-
antred over 9 yearsOut of all the proposed solutions, this is probably be the best one. I'm going to toss this line into a resetErrorlevel.bat script. All in all, the fact that one has to go to such lengths to do something as trivial as clearing a script's error level is yet more proof that the inventor of Windows batch programming should be hunted down and punished harshly. ;)
-
UweBaemayr over 8 yearsThis won't work -- the shell evaluates the entire command line at once, so %errorlevel% will be substituted before the "findstr" command is executed. Use "if errorlevel 1" instead to test for a non-zero errorlevel.
-
Kevin Fegan almost 8 yearsI like this method for setting an arbitrary errorlevel like:
cmd /c "exit /b 9009"
, but it seems a bit overkill for setting it to 0. Wouldn'tver > nul
(an internal command), work just as well with less overhead than loading another copy of the command shell withcmd /c "exit /b 0"
? -
Kevin Fegan almost 8 years@BaiyanHuang - I'm nearly positive
"ver"
is an internal command, but how can you tell from"help"
? Many of the commands listed in"help"
are "external" commands like:Find.exe
,Findstr.exe
,Help.exe
,Subst.exe
,Wmic.exe
,Xcopy.exe
, ... -
Tomasz Gandor over 7 yearsProbably your CI server fails builds if it for example finds the string "error" in the standard output / standard error. You're not resetting the status. You're just silencing the command. For a deeper silence use >nul 2>nul
-
Ross Smith II over 5 yearsThe
/b
is not needed socmd /c “exit 0”
works fine too. -
A Khudairy over 3 yearsI like this one does the job. Thanks. One funny thing im tracing some file and my error level kept changing to 1, finally i found that prompting the user like (set /p id="enter id") changes the errorlevel to 1! ... I had filled the b
-
aschipfl over 3 yearsYes,
cmd /C exit [/B] 0
should be the slowest since a newcmd.exe
instance is opened and closed; the others are all internal commands, so there is no file system access to find executable files…