Batch Script Return Code
Return Code
By default, when the execution of a command line completes, it should return zero when the execution succeeds or non-zero when the execution fails.
When a batch script returns a non-zero value after the execution fails, the non-zero value will indicate what is the error number.
The error number is used to determine the reason for the error and resolve it accordingly.
The following table shows the most common exit codes and their description.
Error Code | Description |
---|---|
0 | Program successfully completed. |
1 | Incorrect function. Indicates that Action has attempted to execute non-recognized command in Windows command prompt cmd.exe. |
2 | The system cannot find the file specified. Indicates that the file cannot be found in specified location. |
3 | The system cannot find the path specified. Indicates that the specified path cannot be found. |
5 | Access is denied. Indicates that user has no access right to specified resource. |
9009 0x2331 | Program is not recognized as an internal or external command, operable program or batch file. Indicates that command, application name or path has been misspelled when configuring the Action. |
221225495 0xC0000017 -1073741801 | Not enough virtual memory is available. It indicates that Windows has run out of memory. |
3221225786 0xC000013A -1073741510 | The application terminated as a result of a CTRL+C. Indicates that the application has been terminated either by the user's keyboard input CTRL+C or CTRL+Break or closing command prompt window. |
3221225794 0xC0000142 -1073741502 | The application failed to initialize properly. Indicates that the application has been launched on a Desktop to which the current user has no access rights. Another possible cause is that either gdi32.dll or user32.dll has failed to initialize. |
Environment Variable %ERRORLEVEL%
The environmental variable %ERRORLEVEL%
contains the return code of the last executed program or script.
By default, the way to check for the ERRORLEVEL
is via the following code.
IF %ERRORLEVEL% NEQ 0 (
do_something
)
It is common to use the command EXIT /B %ERRORLEVEL%
at the end of the batch file to return the error codes from the batch file.
EXIT /B
at the end of the batch file will stop execution of a batch file.
Use EXIT /B <exitcodes>
at the end of the batch file to return custom return codes.
In the batch file, it is always a good practice to use environment variables instead of constant values, since the same variable get expanded to different values on different computers.
Example
Consider the following batch script contained in a file called find.bat. If the file lists.txt is not found, you should set the error level code to 7. Similarly, if the userprofile
variable is not defined, you should set the error level code to 9.
if not exist c:\lists.txt exit 7
if not defined userprofile exit 9
exit 0
Let’s assume we have another file called app.bat that calls find.bat first. If the find.bat file returns an error and sets the error level to a value greater than 0, the program exits.
call Find.cmd
if errorlevel gtr 0 exit
echo “Successful completion”
Output In the above program, the following scenarios can be obtained as output:
- If the file
c:\lists.txt
does not exist, nothing will be displayed in the console output. - If the
userprofile
variable does not exist, nothing will be displayed in the console output. - If both of the above conditions are passed, the command prompt will display the string
"Successful completion"
.