4

After running a MS DOS command in cmd.exe i was checking the exit code using echo %ERRORLEVEL%. However I guess before running this i should clear the variable: %ERRORLEVEL%.

Am i correct? Also how to clear this variable?

Thank you.

R.C
  • 10,417
  • 2
  • 35
  • 48
  • 1
    possible duplicate of [What is the easiest way to reset ERRORLEVEL to zero?](http://stackoverflow.com/questions/1113727/what-is-the-easiest-way-to-reset-errorlevel-to-zero) – twasbrillig Dec 31 '14 at 22:58

2 Answers2

4

As twasbrillig wrote, I think this is already answered in:

What is the easiest way to reset ERRORLEVEL to zero?

And I would mark as best the answer:

cmd /c exit /b 0
Community
  • 1
  • 1
Mayra Delgado
  • 531
  • 4
  • 17
3

Use the verify command. We are causing the ErrorLevel variable to be reset by just performing a command that we know will succeed. So this can be done with any command that you know will succeed. Verify is just the default from what I have seen.

verify >nul

Also, you will want to only clear the variable before calling the command that returns the exit code.

verify >nul
command that returns an exit code
echo %ErrorLevel%
David Ruhmann
  • 11,064
  • 4
  • 37
  • 47
  • 7
    There's no need to clear `%ERRORLEVEL%` before running a command that returns an exit code, because the former content will automatically be replaced with the new exit code. – Ansgar Wiechers Jan 01 '13 at 13:56