Bat Script Error Level
Contents |
Chen - MSFTSeptember 26, 200815 0 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 windows batch scripting errorlevel more
Errorlevel Neq
Does this print? The message 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 pstdin, stdout, stderr Part 5 – If/Then Conditionals Part 6 – Loops Part 7 – Functions Part 8 – Parsing Input Part 9 – Logging Part 10 –
If Errorlevel Equ 0
Advanced Tricks Today we’ll cover return codes as the right way to communicate batch file check errorlevel the outcome of your script’s execution to the world. Sadly, even skilled Windows programmers overlook the importance of return codes. Return batch ping errorlevel 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 is did the script work https://blogs.msdn.microsoft.com/oldnewthing/20080926-00/?p=20743 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 the IF command: IF %ERRORLEVEL% NEQ 0 http://steve-jansen.github.io/guides/windows-batch-scripting/part-3-return-codes.html ( 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 isn’t always pretty, but, it gets the job done. Conditional Execution Using the Return Code Th
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 http://stackoverflow.com/questions/20892882/set-errorlevel-in-windows-batch-file Stack Overflow Questions Jobs Documentation 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 only takes a minute: Sign up Set errorlevel in Windows batch file up vote 8 down vote favorite 1 I am writing a batch script that will loop through each line of a text file, (each line containing a filename) check if the file exists and then runs the script error file and moves it. Here is my batch script: REM Loop through each line of input.txt FOR /F "tokens=1-3 delims=, " %%i IN (./ready/input.txt) DO ( ECHO. ECHO. ECHO. ECHO Check %%i exists, set error flag if it doesnt if not exist .\ready\%%i set errorlevel=2 echo return code is %errorlevel% ECHO Run %%i if it exists if errorlevel 0 call .\ready\%%i ECHO Move %%i to archive if no error occured if errorlevel 0 copy .\ready\%%i .\archive\%mydate%_%mytime%_%%j_%%k_%%i ECHO Copy line of text to the new output.txt bat script error file if an error occured if %errorlevel% NEQ 0 >>output.txt %%i, %%j, %%k ) Here is the output: I do not understand why the "if errorlevel" is not working as expected... if the file does not exist (as in this example where it does not exist) it should NOT try to run the file, it should NOT copy the file, and it should echo a 2 not a 0 Edit 1: I was reading another SO Post regarding "delayed environment variable expansion" I am not sure if this issue is related batch-file share|improve this question edited Jan 8 '14 at 21:10 Mark 2,6961125 asked Jan 2 '14 at 22:11 Kairan 1,680143770 What happens if you do if not "%errorlevel% == "0"? Also, maybe don't set errorlevel itself: batcheero.blogspot.com/2007/07/never-set-errorlevel.html –zero298 Jan 2 '14 at 22:14 @zero298 same thing occurs when using %errorlevel% - cant view that blog as my work is blocking it =( also tried removing the set errorlevel did not cause any change –Kairan Jan 2 '14 at 22:22 One other thing I, why do you have .\file\other why not just file/other –zero298 Jan 2 '14 at 22:42 @zero298 I am just used to using .\ for going to relative subfolders in folder path - personal preference –Kairan Jan 2 '14 at 22:47 set errorlevel=2 <--- never change/set a system variable as it overrides the system variable. Also, when using %variable% within a loop requires the use of delayed expansion and !var