Error handling in batch scripts
Every scripting and programming language contains error handlers, for example Java contains try-catch for error handling. In batch scripts, there is no direct way to do this, but we can create error handlers in batch scripts using the built-in variable of the batch script name %ERRORLEVEL%.
This article will show you how to create a batch script to handle errors and failures. In addition, we will provide some examples to make the topic easier.
Error handling in batch scripts
When a command is executed successfully, it always returns an EXIT CODE indicating whether the command was executed successfully or failed. So, to create error handling in batch files, we can use this exit code in our program.
You can create an error handler in the following general format:
@Echo off
SomeCommand && (
ECHO Message for Success
) || (
ECHO Message for Failure or Error
)
We can also do this by checking %ERRORLEVEL%
a variable called . If the variable contains a value that is not equal to 0, there may have been a problem or error while executing the command. To test the %ERRORLEVEL% variable, you can follow this example code:
@ECHO off
Some Command Here !!!
IF %ERRORLEVEL% NEQ 0 (Echo Error found when running the command &Exit /b 1)
You must note that the keyword NEQ stands for Not Equal To. The variable %ERRORLEVEL% will only contain a non-zero value if there is a problem or error in the code.
Example with error
Below, we have shared an example. We will run a batch file named Your_file.bat from a location.
We intentionally deleted the file from the directory. So this is a wrong command.
Our example code is:
@echo off
ECHO Running a Batch file
CD G:\BATCH\
CALL Your_file.bat
IF errorlevel 1 GOTO ERROR
ECHO The file run successfully.
GOTO EOF
:ERROR
ECHO The file didn't run successfully.
CMD /k
EXIT /b 1
:EOF
Now, as the file does not exist in the directory, it will show error and when you run the code shared above, you will get the following output.
Output:
Running a Batch file
The system cannot find the path specified.
'Your_file.bat' is not recognized as an internal or external command,
operable program or batch file.
The file didn't run successfully.
An example of code that runs successfully without errors
In the above example, we intentionally made a mistake in the code to understand how the code works. If we correct it like this:
@echo off
ECHO Running a Batch file
CALL "G:\BATCH\Yourfile.bat"
IF errorlevel 1 GOTO ERROR
ECHO The file runs successfully.
GOTO EOF
:ERROR
ECHO The file didn't run successfully.
CMD /k
EXIT /b 1
:EOF
Then we will get output like this:
Running a Batch file
This is from the first file
The file runs successfully.
Keep in mind that all the commands we discuss here only work in the Windows Command Prompt, or CMD environment.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Waiting for commands in a Windows batch file to finish executing
Publish Date:2025/03/20 Views:98 Category:OPERATING SYSTEM
-
There are multiple commands and installation processes in a batch file, which usually takes some time to complete. However, when a batch file is run, it does not wait for the command process to complete; it executes all the commands line by
Redirecting output to a text file from within a batch file
Publish Date:2025/03/20 Views:186 Category:OPERATING SYSTEM
-
This article will explain different ways to redirect output from a text file. Redirection operators in batch scripts We can redirect the output of a batch file to a text file using redirection operators. Redirection operators redirect the i
在批处理脚本中使用 IF ELSE 和 GOTO
Publish Date:2024/03/15 Views:113 Category:编程语言
-
本教程将展示我们如何在批处理脚本中组合 IF ELSE 和 GOTO 命令。