Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Shell Scripting
  • Kali Linux
  • Ubuntu
  • Red Hat
  • CentOS
  • Docker in Linux
  • Kubernetes in Linux
  • Linux interview question
  • Python
  • R
  • Java
  • C
  • C++
  • JavaScript
  • DSA
Open In App
Next Article:
Servlet - Debugging
Next article icon

Batch Script - Debugging

Last Updated : 28 Feb, 2022
Comments
Improve
Suggest changes
Like Article
Like
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).

Debugging - It 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:


Next Article
Servlet - Debugging

S

shubhambhugra234
Improve
Article Tags :
  • Linux-Unix
  • Geeks Premier League
  • Geeks-Premier-League-2022
  • Batch-script

Similar Reads

  • Batch Script - Logging
    Batch Script is a file that consists of various commands which need to be sequentially executed. It is not like coding and is not frequently used, in order to communicate with the OS through a command prompt, the user can use Batch Script. The commands are known as Batch commands. Logging refers to
    2 min read
  • Batch Script - Return code
    Return codes are the codes returned by programs when they are executed. If the command line is successful, it should return zero; if it is not successful, it should return non-zero. If the test fails, a non-zero value indicates the error number, and the user can attempt to resolve it by navigating t
    3 min read
  • Batch Script - String length
    In this article , we are going to learn how to find length of any String using Batch Script. Batch Script :@echo off set str=Geeks For Geeks call :strLen str strlen echo String is %strlen% characters long pause exit /b :strLen setlocal enabledelayedexpansion :strLen_Loop if not "!%1:~%len%!"=="" set
    2 min read
  • Servlet - Debugging
    One of the most difficult components of building servlets is testing and debugging. Because servlets include a lot of client/server interaction, they're prone to errors–though they're hard to come by. Because servlets operate inside a strongly multithreaded and typically complicated web server, they
    6 min read
  • Batch Script - Device Console
    A batch script is a text file that includes various sequential commands. It is used to evaluate repetition routines in Windows,  OS/2, and DOS operating systems and is used in network and system administration. A batch file refers to a file that stores command in a serial order.  The library has bee
    3 min read
  • Batch Script - Align Right
    We can use Batch scripting for manipulating the data. We have certain commands and filters to manipulate and edit certain pieces of data very easily for better visualization. Align Right in Batch scripting is one of the commands/filters that helps in aligning or arranging text in a desired manner. U
    3 min read
  • Batch Script - Input / Output
    In this article, we are going to learn how to take input from users using Batch Script. Taking User Input:@echo off echo Batch Script to take input. set /p input= Type any input echo Input is: %input% pauseExplanation :The first 'echo' command is used to print a string.In the next line, 'set /p' com
    1 min read
  • Batch Script - Right String
    In this article, we are going to learn how to use the concept of Right String using Batch Script. Right String uses the concept of negative indexing. We have to extract the characters using :~ followed by the negative index of the character from which we want to print a substring from the main strin
    2 min read
  • Batch Script - Mid String
    In this article , we are going to learn how to use the concept of Mid String using Batch Script. Using the concept of 'Mid String' we are extracting out a sub string between two indices of any given string. Batch Script :@echo off set str=GeeksforGeeks echo %str% set str=%str:~5,-5% echo %str% pause
    2 min read
  • Batch Script - Left String
    In this article, we are going to study Left string in Batch Script. In Batch Script, the Left string is used to extract the characters from the beginning of the string by giving position 0 and a length using:~ while expanding a variable content. Example 1: Batch script set str=GeeksForGeeks echo.%st
    1 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences