Error Code Command Line
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 windows command line error code more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags command line color codes Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, command prompt codes helping each other. Join them; it only takes a minute: Sign up How do I get the application exit code from a Windows command line? up vote 467 down vote favorite 95 I am running a program and
Matrix Code Command Prompt
want to see what its return code is (since it returns different 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,57331622 5 Also asked on SuperUser: How to check the exit code of the last command in batch file? –Deanna Jun 24 command prompt star wars '13 at 11:42 add a comment| 6 Answers 6 active oldest votes up vote 575 down vote accepted 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.4k24105204 answered Dec 2 '08 at 18:07 Samuel Renkert 6,83821626 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 %ERRORL
stdin, stdout, stderr Part 5 – If/Then Conditionals Part 6 – Loops Part 7 – Functions Part 8 – Parsing Input Part 9 – Logging cmd exit /b Part 10 – Advanced Tricks Today we’ll cover return codes as the right
Cmd Set Errorlevel
way to communicate the outcome of your script’s execution to the world. Sadly, even skilled Windows programmers overlook the importance
Cmd Errorlevel
of return codes. Return Code Conventions By convention, command line execution should return zero when execution succeeds and non-zero when execution fails. Warning messages typically don’t effect the return code. What matters http://stackoverflow.com/questions/334879/how-do-i-get-the-application-exit-code-from-a-windows-command-line is did the script work or not? Checking Return Codes In Your Script Commands The environmental variable %ERRORLEVEL% contains the return code of the last executed program or script. A very helpful feature is the built-in DOS commands like ECHO, IF, and SET will preserve the existing value of %ERRORLEVEL%. The conventional technique to check for a non-zero return code using the NEQ (Not-Equal-To) operator of http://steve-jansen.github.io/guides/windows-batch-scripting/part-3-return-codes.html the IF command: IF %ERRORLEVEL% NEQ 0 ( REM do something here to address the error ) Another common technique is: IF ERRORLEVEL 1 ( REM do something here to address the error ) The ERRORLEVEL 1 statement is true when the return code is any number equal to or greater than 1. However, I don’t use this technique because programs can return negative numbers as well as positive numbers. Most programs rarely document every possible return code, so I’d rather explicity check for non-zero with the NEQ 0 style than assuming return codes will be 1 or greater on error. You may also want to check for specific error codes. For example, you can test that an executable program or script is in your PATH by simply calling the program and checking for return code 9009. SomeFile.exe IF %ERRORLEVEL% EQU 9009 ( ECHO error - SomeFile.exe not found in your PATH ) It’s hard to know this stuff upfront – I generally just use trial and error to figure out the best way to check the return code of the program or script I’m calling. Remember, this is duct tape programming. It is
will exit only the script (or subroutine) but not CMD.EXE exitCode Sets the %ERRORLEVEL% to a numeric number. If quitting CMD.EXE, set the process exit code no. To close an interactive command prompt, the keyboard shortcut ALT + F4 is an alternative http://ss64.com/nt/exit.html to typing EXIT. Errorlevels EXIT /b has the option to set a specific errorlevel, 0 for sucess, 1 or greater for an error. EXIT without an ExitCode acts the same as goto:eof and will not alter the %ERRORLEVEL% You should http://superuser.com/questions/123008/get-exit-status-of-last-executed-command-in-windows never attempt to directly write to the %ERRORLEVEL% variable, (SET errorlevel...) instead use the EXIT command as a safe way to alter the value of the internal ERRORLEVEL pseudo variable. When EXIT /b used with FOR /L, the execution of command line the commands in the loop is stopped, but the loop itself continues until the end count is reached. This will cause slow performance if the loop is (pointlessly) counting up to a large number. In the case of an infinite loop, this EXIT /b behaviour will cause the script to hang until manually terminated with Ctrl + C Exiting nested FOR loops, if EXIT /b can be used to exit a FOR loop that is nested within another FOR loop. This error code command will only work if the inner FOR loop is contained in a separate subroutine, EXIT /b terminates the subroutine. Examples Exit if a required file is missing @Echo Off If not exist MyimportantFile.txt Exit /b Echo If we get this far the file was found Set the errorlevel to 5 @Echo Off call :setError echo %errorlevel% goto :eof :setError Exit /B 5 Using EXIT /b to exit a nested FOR loop (so skipping the values X,Y andZ), but still continuing the main loop: @Echo Off Setlocal For %%A in (alpha beta gamma) DO ( Echo Outer loop %%A call :inner ) goto :eof :inner For %%B in (U V W X Y Z) DO ( if %%B==X ( exit /b 2 ) echo Inner loop Outer=%%A Inner=%%B ) EXIT is an internal command. “Making music is not about a place you go. It’s about a place you get out of. I’m underwater most of the time, and music is like a tube to the surface that I can breathe through. It’s my air hole up to the world. If I didn’t have the music I’d be under water, dead” ~ Fiona Apple Related: VERIFY - Provides an alternative method of raising an errorlevel without exiting TSKILL - End a running process Powershell: Exit - Exit Powershell or break - Exit a program loop Equivalent bash command (Linux): break - Exit from a loop © Copyright SS64.com 1999-2016 Some rights reserved
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 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 Get exit status of last executed command in Windows? up vote 9 down vote favorite 2 Is there something like Unix's echo $? in Windows? windows command-line unix share|improve this question asked Mar 23 '10 at 3:44 Steven 82942534 add a comment| 1 Answer 1 active oldest votes up vote 12 down vote accepted Use the errorlevel environment variable: echo %errorlevel% This question on stackoverflow has more detailed information. share|improve this answer answered Mar 23 '10 at 3:51 mindless.panda 4,21793461 add a comment| Your Answer draft saved draft discarded Sign up or log in Sign up using Google Sign up using Facebook Sign up using Email and Password Post as a guest Name Email Post as a guest Name Email discard By posting your answer, you agree to the privacy policy and terms of service. Not the answer you're looking for? Browse other questions tagged windows command-line unix or ask your own question. asked 6 years ago viewed 8558 times active 3 years ago Related 7Exit program in windows command prompt2Command Line replacements for Windows Vista21Is there a Windows equivalent of the Unix “strings” command?7Unix commands to get last modified date and size of file/folder (NOT LS)0how to get the status of commands executed inside ssh connection2In Windows Command Prompt, how to get the last command that started with some letters?0How to list file names on Windows as the Unix find comman