If Error Level
Contents |
Chen - MSFTSeptember 26, 200815 Share 0 0 The command interpreter cmd.exe has a concept known as the error level, which is the exit code of the program most recently run. You can test the error level with the IF ERRORLEVEL command: IF ERRORLEVEL 1 ECHO error level is 1 or more
Echo Errorlevel
anything else to mean failure. In addition to this internal state, you can, if you wish, create an environment variable with the name ERRORLEVEL, in the same way that you can create an environment variable called FRED. But, as with FRED, that variable won't have any effect on the error level. rem this next command sets the error level to zero CMD /C EXIT 0 set ERRORLEVEL=1 if ERRORLEVEL 1 echo Does this print? The message errorlevel codes is not printed because the ERRORLEVEL environment variable has no effect on the error level. It's just a variable whose name happens to coincide with a command processor concept. set BANKBALANCE=$1,000,000.00 "Hey, when I tried to withdraw the money, I got an insufficient funds error. What am I doing wrong?" Now, it does happen to be the case that if command extensions are enabled and you say %ERRORLEVEL%, then the command processor first looks for an environment variable called ERRORLEVEL, and if it can't find one, then it replaces %ERRORLEVEL% with the current value of the internal error level value. It's a fallback step, in the same way that your neighbor is a fallback delivery location if you aren't home. If you file a change-of-address form for yourself, that doesn't affect packages sent to your neighbor. The same behavior can be seen with %CD%: If you did not explicitly set an environment variable called CD, then %CD% expands to the command processor's current directory. But you can't change directories by saying set CD=C:\Windows. I can think of a few reasons why this feature may have been added. So you can include the error level in a log file: ECHO error level is %ERRORLEVEL%>logfile
So you can perform other types of tests against the error level, for example, to perform an equality test: IF %ERRORLEVEL% EQU 1 echo Different! Buthere for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings set errorlevel and policies of this site About Us Learn more about Stack Overflow
Errorlevel 9009
the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation
If Errorlevel Else
Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it https://blogs.msdn.microsoft.com/oldnewthing/20080926-00/?p=20743 only takes a minute: Sign up IF, CALL, EXIT and %ERRORLEVEL% in a .bat up vote 4 down vote favorite Can anyone please help me understand the behaviour of %ERRORLEVEL% variable and why it's not being set after a CALL while being inside an IF, i.e. the ECHO %ERRORLEVEL%.2 line? @ECHO OFF SET ERRORLEVEL VERIFY > NUL ECHO %ERRORLEVEL%.0 IF http://stackoverflow.com/questions/24866477/if-call-exit-and-errorlevel-in-a-bat ERRORLEVEL 1 ECHO SNAFU IF %ERRORLEVEL% == 0 ( ECHO %ERRORLEVEL%.1 CALL :FOO ECHO %ERRORLEVEL%.2 IF ERRORLEVEL 42 ECHO 42.3 ) GOTO :EOF :FOO EXIT /B 42 GOTO :EOF STDOUT C:\Users\Ilya.Kozhevnikov\Dropbox>foo.bat Environment variable ERRORLEVEL not defined 0.0 0.1 0.2 42.3 However, without IF the %ERRORLEVEL% variable is set as expected. @ECHO OFF SET ERRORLEVEL VERIFY > NUL ECHO %ERRORLEVEL%.0 IF ERRORLEVEL 1 ECHO SNAFU REM IF %ERRORLEVEL% == 0 ( ECHO %ERRORLEVEL%.1 CALL :FOO ECHO %ERRORLEVEL%.2 IF ERRORLEVEL 42 ECHO 42.3 REM ) GOTO :EOF :FOO EXIT /B 42 GOTO :EOF STDOUT C:\Users\Ilya.Kozhevnikov\Dropbox>foo.bat Environment variable ERRORLEVEL not defined 0.0 0.1 42.2 42.3 windows batch-file share|improve this question asked Jul 21 '14 at 13:36 Ilya Kozhevnikov 6,31011644 possible duplicate of windows batch SET inside IF not working –Laf Jul 21 '14 at 13:46 See the question I've marked as being a duplicate of. This explains exactly what is happening with your script, and what you have to do to fix the problem. –Laf Jul 21 '14 at 13:47 add a comment| 2 Answers 2 active olde
codes set by resource kit utilities are not always consistent, they can vary between machines with different Service packs/Resource kit updates applied. Some utilities will return negative numbers as an exit code. If you attempt http://ss64.com/nt/errorlevel.html to execute a non-existent command %ERRORLEVEL% = 9009 Detecting Errors In the CMD shell the exit code is made available via the %ERRORLEVEL% variable or via IF ERRORLEVEL ... IF ERRORLEVEL n statements should be read as IF Errorlevel >= number i.e. IF ERRORLEVEL 1 will return TRUE when the ERRORLEVEL is greater than or equal to 1 IF NOT ERRORLEVEL 1 means if ERRORLEVEL is less than 1 (Zero or negative). When ending if error a subroutine, you can use EXIT /b N to set a specific ERRORLEVEL N. You should never attempt to write to the %ERRORLEVEL% variable because the value you set will create a user variable named ERRORLEVEL which then takes precedence over the internal pseudo variable ERRORLEVEL. When an external command is run by CMD.EXE, it will detect the executable's return code and set the ERRORLEVEL to match. In most cases the ERRORLEVEL will be the if error level same as the exit code, but there are a few buggy cases where this fails. The Exit Codes can be detected directly with redirection operators (Success/Failure ignoring the ERRORLEVEL) Some commands don't follow the rules Commands that do NOT affect the ERRORLEVEL: BREAK, ECHO, ENDLOCAL, FOR, IF, PAUSE, REM, RD/RMDIR, SET, TITLE Commands that will set but not clear an ERRORLEVEL: CLS, GOTO, KEYS, POPD, SHIFT Commands that set an Exit Code but not the ERRORLEVEL: RD/RMDIR Commands that set an ERRORLEVEL but not the Exit Code (SO explanation): MD/MKDIR Batch files You can make a batch file return a non-zero exit code by using the EXIT command. Exit 0 Exit /B 5 To force an ERRORLEVEL of 1 to be set without exiting, run a small but invalid command like COLOR 00 There is a key difference between the way .CMD and .BAT batch files set errorlevels: An old .BAT batch script running the 'new' internal commands: APPEND, ASSOC, PATH, PROMPT, FTYPE and SET will only set ERRORLEVEL if an error occurs. So if you have two commands in the batch script and the first fails, the ERRORLEVEL will remain set even after the second command succeeds. This can make debugging a problem BAT script more difficult, a CMD batch script is more consistent and will set ERRORLEVEL after eve