What are Batch file exit codes or errorlevels?

To check whether a batch file/script has been successfully executed, a condition check is generally included as the last command in the script. This condition checks the status of execution, and depending on whether or not the script was executed successfully, returns a value. A successful script execution returns a 0 while an unsuccessful one returns a non-zero value that is interpreted as an Error Code or an errorlevel.

Steps to return exit codes (errorlevels) for batch files:

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.
  • Environment variable %ERRORLEVEL% contains the latest errorlevel in the batch file,which is the latest error codes from the last command executed. To know about Environment variable see the below note.
Note: Environment variables are a set of dynamic named values that can affect the way, running processes will behave on a computer. For example, an environment variable with a standard name can store the location that a particular computer system uses to store user profile this may vary from one computer system to another.

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.


Batch file for Copying File to a Folder
md "C:manageengine"
copy "\\sharename\foldername\samplefile.txt" "C:\manageengine"
exit /b %ERRORLEVEL%