What is the easiest way to reset ERRORLEVEL to zero?

87,228

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;

https://www.dostips.com/forum/viewtopic.php?t=5542

Share:
87,228

Related videos on Youtube

user95319
Author by

user95319

Updated on July 05, 2022

Comments

  • user95319
    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
      Dykam almost 15 years
      The build doesn't really fail, only the IDE does look like it.
    • Arun
      Arun almost 11 years
      I 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
    user95319 almost 15 years
    I 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
    Jason Kresowaty almost 15 years
    I'll give you that, but where is ver.exe?
  • user95319
    user95319 almost 15 years
    I can't find "ver.exe" or "ver.com" either. I don't know how to explain that.
  • madoki
    madoki over 12 years
    not 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
    madoki over 12 years
    thanks, 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
    AnneTheAgile about 11 years
    At batch command line, "ver" returns the MS windows version, eg "Microsoft Windows [Version 6.1.7601]".
  • Alyssa Haroldsen
    Alyssa Haroldsen almost 11 years
    even more fancy: you can use <some failing command> || cmd /c "exit /b 0" as a one liner.
  • ed9w2in6
    ed9w2in6 about 10 years
    if you run help in command shell, you will see ver is also a built in command
  • jeb
    jeb over 9 years
    It's easy but it´s a really bad idea, as this creates a variable named errorlevel which overlays the internal pseudo variable errorlevel
  • cirrus
    cirrus over 9 years
    Crikey, you're right! Wow, looks like I've been dodging bullets for years :)
  • antred
    antred over 9 years
    Out 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
    UweBaemayr over 8 years
    This 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
    Kevin Fegan almost 8 years
    I 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't ver > nul (an internal command), work just as well with less overhead than loading another copy of the command shell with cmd /c "exit /b 0" ?
  • Kevin Fegan
    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
    Tomasz Gandor over 7 years
    Probably 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
    Ross Smith II over 5 years
    The /b is not needed so cmd /c “exit 0” works fine too.
  • A Khudairy
    A Khudairy over 3 years
    I 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
    aschipfl over 3 years
    Yes, cmd /C exit [/B] 0 should be the slowest since a new cmd.exe instance is opened and closed; the others are all internal commands, so there is no file system access to find executable files…