Batch file return code

In this tutorial, you will learn about batch file return code, what is a return code and how is it used in batch file programming.

Batch files return code: Introduction
Error level in batch file
Batch files return code: Example

return code batch file

Batch file return code – Introduction

So, a return code is a code returned after the execution of a program. By default, the command line execution should return zero when execution succeeds and non-zero when execution fails. When execution fails, the returned non-zero value indicates the corresponding error number and user can go through that error to resolve it.

These returned error codes are also called exit codes. Almost all application or utilities return the exit code when they are complete or they terminate.

Following are some of the non-zero numbers (exit codes) returned from programs with their corresponding errors.

Exit Code/Error Code Details/Description
0 Program successfully executed.
1 Incorrect function. Indicates that Action has attempted to execute non-recognized command in Windows command prompt cmd.exe.
2 Indicates that the system cannot find the file in that specified location.
3 Indicates that the system cannot find the specified path.
5 Access is denied. So the user has no access right to specified resource.
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.
Access violation.
Indicates that the executed program has terminated abnormally or crashed.
Not enough virtual memory is available.
Indicates that Windows has run out of memory.
The application terminated as a result of a CTRL+C. Indicates that the application has been terminated either by user’s keyboard input CTRL+C or CTRL+Break or closing command prompt window.
The application failed to initialize properly. Indicates that the application has been launched on a Desktop to which current user has no access rights. Another possible cause is that either gdi32.dll or user32.dll has failed to initialize.
Not enough virtual memory is available.

It indicates that Windows has run out of memory.

Stack buffer overflow / overrun. Error can indicate a bug in the executed software that causes stack overflow, leading to abnormal termination of the software.
Unhandled exception in .NET application. More details may be available in Windows Event log.

Now that we know about exit codes, the next thing is to know or check the return codes in our script commands. For that, environment variable %ERRORLEVEL% is used.


Error Level In Batch File

The environmental variable %ERRORLEVEL% contains the return code or the latest error level in the batch file, which is the latest error codes from the last command executed.

A common way of checking Error levels using %ERRORLEVEL% variable is:

IF %ERRORLEVEL% NEQ 0 Echo An error was found
IF %ERRORLEVEL% EQU 0 Echo No error found

Besides this, the command EXIT /B %ERRORLEVEL% at the end of the batch file also returns the error codes from the batch file.

EXIT /B < exitcodes > is used to return custom return codes.

Batch file return code example

Here is a simple example to illustrate how to check error codes in a batch file.

IF EXIST D:/abc.txt EXIT 9

So, basically what this script does is, it checks D: for file abc.txt. If the file exist, it will return error code zero and terminate and if the file abc.txt doesn’t exist in D: , then it will return error code 9 and terminate.