Open In App

Batch Script – Debugging

Last Updated : 28 Feb, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Batch Script is a text file that includes a definite amount of operations or commands that are performed in order. It is used in system networks as well as in system administration. It also eliminates a specific repetitive piece of work in various Operating Systems like DOS(Disk Operating System).

DebuggingIt is a process of removing errors from a program or computer in order to perform better.

Users can Identify the error in the batch script in the following ways:-

  • By adding pause command.
  • By working with the echo command.
  • Employ/Logging the error message to another file.
  • By using an environment variable (%ERRORLEVEL%) to detect errors.

By adding pause command: One way to debug batch file is by executing the pause command or operation and halting the executing program if any kind of error is found or occurred then a developer can easily fix the issue by restarting the process. In the below instance, batch script is paused as input value is compulsory to provide.

@echo off  
if [%2] == [] (  
  echo input value not provided  
  goto stop  
) else (  
  echo "Correct value"      
)  
:stop  
pause 

Working with the echo command: It’s quite an easy and basic option to debug the batch file. It will pop up a message wherever the error occurred.  In the below example the echo command is used to print the numbers as well as also in the place where the chance of error is higher.

@echo off  
if [%1] == [] (  
  echo input value not provided  
  goto stop  
)  
rem Print num  
for /l %%n in (2,2,%1) do (  
  echo "Numbers: " ,%%n  
)  
:stop  
pause 

Employ/Logging the error message to another file: It is hard to solve the issue just by looking at it in the command prompt window using the echo command. So we need to employ the errors into a separate file so that developers can easily look at the errors and can solve them.

Following is the eg of sample file:

net statistics /Server

Execute this command in your command line prompt on your PC:

C:\>sample.bat > samplelog.txt 2> sampleerrors.txt

The file sampleerrors .txt will display errors as given below:

By using an environment variable (%ERRORLEVEL%) to detect errors: The environmental variable %ERRORLEVEL% contains the return code or the latest error level in the batch file. This variable returns 1 in case of an incorrect function, 0 if it executes successfully, 2 shows that it can’t find a specified file in a particular location, 3 represents that system can’t able to find mentioned path, and 5 shows that access got denied. Error codes are also known as exit codes. At the end of the file EXIT command also returns the errors from a batch file. 

Syntax of above is given below:

IF %ERRORLEVEL% NEQ 0 Echo "Error was found"
IF %ERRORLEVEL% EQU 0 Echo "No error found"

You can also see the log file along with errors: samplelog.txt:


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads