Check Error Code Batch
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with batch file check for error code us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow batch error code 1 Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up
Return Error Code From Batch File
Get error code from within a batch file up vote 26 down vote favorite 5 I have a batch file that runs a couple executables, and I want it to exit on success, but stop if the exit code <> 0. How
Batch Check Exit Code
do I do this? windows batch-file exit-code share|improve this question asked Aug 10 '10 at 18:12 Dlongnecker 1,55211437 add a comment| 2 Answers 2 active oldest votes up vote 30 down vote accepted Sounds like you'll want the "If Errorlevel" command. Assuming your executable returns a non-0 exit code on failure, you do something like: myProgram.exe if errorlevel 1 goto somethingbad echo Success! exit :somethingbad echo Something Bad Happened. Errorlevel checking is done as a greater-or-equal check, so any non-0 exit value will trigger the exit code batch script jump. Therefore, if you need to check for more than one specific exit value, you should check for the highest one first. share|improve this answer answered Aug 10 '10 at 18:20 Hellion 1,2481527 2 Also, since around Windows 2000, there's a "virtual" environment variable called %ERRORLEVEL% that can be tested with ==, EQU, LSS, etc. –Jim Davis Aug 10 '10 at 19:13 Perfect! Thanks. –Dlongnecker Aug 10 '10 at 21:24 2 Oh, for posterity, to make it stop on error, after :somethingbad, use a "pause" command –Dlongnecker Aug 11 '10 at 20:08 2 You don't want to use exit in there as that kills the shell. Either use goto :EOF or exit /b so that just the batch file terminates. –Joey Aug 11 '10 at 21:44 @Jim: You should always delete that variable with set ERRORLEVEL= at the start of your batch if you intend to use it, as the function of the pseudo-variable can be shadowed by creating an actual variable with that name. And since the environment is passed from the parent process ... you can never be sure. –Joey Aug 11 '10 at 21:45 | show 1 more comment up vote 23 down vote You can also use conditional processing symbols to do a simple success/failure check. For example: myProgram.exe && echo Done! would print Done! only if myProgram.exe returned with error level 0. myProgram.exe || PAUSE would cause the batch file to pause if myProgram.exe returns a non-zero error level.
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting
Batch File Check Errorlevel
ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join cmd exit /b the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a batch set errorlevel minute: Sign up How do I get the application exit code from a Windows command line? up vote 465 down vote favorite 95 I am running a program and want to see what its return code is (since it returns different http://stackoverflow.com/questions/3452046/get-error-code-from-within-a-batch-file codes based on different errors). I know in Bash I can do this by running echo $? What do I do when using cmd.exe on Windows? windows command-line process cmd exit-code share|improve this question asked Dec 2 '08 at 18:04 Skrud 3,56331622 5 Also asked on SuperUser: How to check the exit code of the last command in batch file? –Deanna Jun 24 '13 at 11:42 add a comment| 6 Answers 6 active oldest votes up vote 572 down vote accepted http://stackoverflow.com/questions/334879/how-do-i-get-the-application-exit-code-from-a-windows-command-line A pseudo environment variable named errorlevel stores the exit code: echo Exit Code is %errorlevel% Also, the if command has a special syntax: if errorlevel See if /? for details. Example @echo off my_nify_exe.exe if errorlevel 1 ( echo Failure Reason Given is %errorlevel% exit /b %errorlevel% ) Warning: If you set an environment variable name errorlevel, %errorlevel% will return that value and not the exit code. Use (set errorlevel=) to clear the environment variable, allowing access to the true value of errorlevel via the %errorlevel% environment variable. share|improve this answer edited Aug 9 at 16:11 Dave Jarvis 16.3k24105204 answered Dec 2 '08 at 18:07 Samuel Renkert 6,80821626 17 If you're running directly from a Windows command line and always seeing 0 returned, see Gary's answer: stackoverflow.com/a/11476681/31629 –Ken Aug 30 '12 at 13:51 1 Also if you're in powershell you can use echo Exit Code is $LastExitCode –Brandon Pugh Jan 16 '14 at 19:47 4 Note: "errorlevel 1" is true if errorlevel >= 1. So "errorlevel 0" will match everything. See "if /?". Instead, you can use "if %ERRORLEVEL% EQU 0 (..)". –Curtis Yallop Jul 29 '14 at 16:06 Found cases where %ERRORLEVEL% is 0 even though an error occurred. Happened when checking %ERRORLEVEL% in a cmd file. Trying start /wait didn't work. The only thing that worked is if errorlevel 1 (...) –AlikElzin-kilaka Apr 13 '15 at 12:57 %errorlevel% seems not to be available in powe
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about http://superuser.com/questions/194662/how-to-check-the-exit-code-of-the-last-command-in-batch-file Stack Overflow the company Business Learn more about hiring developers or posting ads with us Super User Questions Tags Users Badges Unanswered Ask Question _ Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute: Sign up Here's how it works: Anybody can ask a question Anybody can answer The best answers are voted up and rise to the top error code How to check the exit code of the last command in batch file? up vote 54 down vote favorite 5 Inside a batch file on Windows, I use 7-zip like this: ...\right_path\7z a output_file_name.zip file_to_be_compressed How could I check the exit code of 7z and take the appropriate action ? windows-xp batch share|improve this question asked Oct 1 '10 at 4:47 Misha Moroshko 1,63861628 1 Also asked on Stackoverflow: How batch file check do I get the application exit code from a Windows command line? –Deanna Jun 24 '13 at 11:42 add a comment| 2 Answers 2 active oldest votes up vote 62 down vote accepted Test for a return code greater than or equal to 1: if ERRORLEVEL 1 echo Error or if %ERRORLEVEL% GEQ 1 echo Error or test for a return code equal to 0: if %ERRORLEVEL% EQU 0 echo OK You can use other commands such as GOTO where I show echo. share|improve this answer edited Oct 1 '10 at 5:27 answered Oct 1 '10 at 4:58 Dennis Williamson 57.4k10100135 I tried your code. I got the following error: 0 was unexpected this time. –Misha Moroshko Oct 1 '10 at 5:13 2 @Misha: You may have tried it with the percent signs the way I originally posted it. Try it without them or try the other versions I added. –Dennis Williamson Oct 1 '10 at 5:24 Great, thanks a lot !! –Misha Moroshko Oct 1 '10 at 11:53 2 Found cases where %ERRORLEVEL% is 0 even though an error occurred. Happened when checking %ERRORLEVEL% in a cmd file. Trying start /wait didn't work. The only thing that worked is if errorlevel 1 (...) &nd