Errorlevel is a system variable in Bash that contains the status or error code of the last command execution.
If it returns 0, the command is successful. If it returns non-zero, the command failed with an error, returns an exit(error) code.
REM error level failed and succesfull case
We have used two commands in the batch script:
lsis not a valid command in DOS and throws an error “ls command is not internal or external”, so errorlevel returns
diris a valid command and executes successfully, so it returns errorlevel as
Batch IF Conditional checking Error Level Example
You can use the
errorlevel variable in conditional expressions to execute conditional branches based on error or successful command execution.
To check the
errorlevel, use the
equ operator to check if the exit code is zero for successful operation.
if %errorlevel% equ 0 (
) else (
echo Failed. Errorlevel: %errorlevel%.
Batch File Error Codes
Errorlevel always returns an error code in a batch file. It is also called an exit code.
|Function name is invalid
|File not found
|file path not found
|Permission issue and Access is denied
|error in executon non executable command
|Invalid file descriptor
|Not enough permissions to access the file
|Environment variables not configured
|Command is not internal and external command
Errorlevel and ERRORLEVEL are the same.
These variables are not changed by the user, not recommended.