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:
Bash Scripting - Working of Alias
Next article icon

Basics of Batch Scripting

Last Updated : 29 Sep, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

Batch Scripting consists of a series of commands to be executed by the command-line interpreter, stored in a plain text file. It is not commonly used as a programming language and so it is not commonly practiced and is not trending but its control and dominance in the Windows environment can never be neglected. Almost every task and every action can be performed and executed by a simple sequence of commands typed on the Windows Command Prompt. 

Batch Script execution

There are 2 ways to execute a batch script.

  • Type the batch script in the command prompt.
  • Write the code of script in a file and execute it through the command prompt.

Typing commands again and again on the terminal can be a very tedious task to do if we have a very lengthy code. So option 2 is generally preferred to create batch files.

Creating Batch Files

Steps to create a Batch file are pretty simple:-

  1. Create a new text file with a ‘.txt‘ extension.
  2. Now rename this file with extension as ‘.bat‘ this creates a Batch file.
  3. Now open this .bat file in any text editor and start scripting.

To begin scripting we must be aware of the commands of the batch interface. The commands of Batch are sometimes similar to Linux Scripting commands.

Batch Commands

Basic batch commands are all case insensitive and can be used to perform a specific set of instructions:-

  • DIR – The ‘dir’ command is used to get all the directories, sub-directories, and files present in the current working directory.
  • CD – The ‘cd’ command is used to change the current working directory.
  • VER – The ‘ver’ command tells the version of the user’s Windows.
  • CLS – The ‘cls’ command is used to clear the screen of the command prompt.
  • ECHO – The ‘echo’ command is by default ‘on’ but if we turn it off by ‘echo off’ it turns off prompt till the time ‘echo on’ is passed.
  • @ – The ‘@’ if used before any command hides which command is running.
  • @ECHO OFF – This commands serves as the start point to any basic batch script as it hides the prompt with ‘echo off’ and hides ‘echo off’ command with ‘@’.
  • HELP – This command tells us all about the commands available in the cmd. It runs only if the cmd is run as an administrator.
Batch commands

How to execute a batch command through cmd(command prompt)

Data Types in Batch

  • Integers – Batch supports the whole set of positive and negative integers
  • Strings – Unlike most programming languages we rarely use (“”) double-quotes here but we use ‘echo‘ command to print strings

Note: Batch doesn’t support floating-point values i.e. values with precision.

Variables in Batch Scripting

A variable is an entity that stores a specific value and allows the user to perform any set of instructions on it. To create variables we use the command “SET” command. A variable, unlike many programming languages, can be assigned simply without specifying any data type to it.

SET my_variable=Hello World

To print this variable we need to use the command ECHO but with a slight variation. Since echo  prints both strings and variables to print string we simply write the string after ECHO as

ECHO Hello World

But to print a variable we use ECHO in a different way bypassing the variable names inside two percent signs (%)  so that variable name doesn’t become a string-

ECHO %my_variable%

Working with Batch Scripts

Creating our own Batch Scripts

Example 1: To print “GeeksForGeeks” on the command prompt with and without using a variable.

Without using a variable 

ECHO GeeksForGeeks

With a variable

SET my_var=GeeksForGeeks ECHO %my_var%

creating-batch-script

Arithmetic Operators in a Batch Script

List of operators :

SET /A sum=1+1     ::addition operator ECHO %sum%      SET /A mul=7*9     ::multiplication operator ECHO %mul% SET /A div=9/3     ::Division operator ECHO %div% SET /A assign=10   ::Assignment operator ECHO %assign% SET /A assign+=15  ::Increment then assignment operator ECHO %assign% SET /A mod= 10%3   ::Modulus/Remainder operator ECHO %mod%
arithmatic-operators-in-batch-script

Demonstration of all arithmetic operators 



Next Article
Bash Scripting - Working of Alias

I

ishitably100
Improve
Article Tags :
  • Linux-Unix
  • Misc
Practice Tags :
  • Misc

Similar Reads

  • Batch Script - Aliases
    Be it Linux, macOS, or Windows, we sometimes need to use the terminal or the command line to perform certain commands. If in such situations, we find ourselves repeating a few long commands we can use an alias to save time and make it easier. In windows, we can create an alias as a batch command fro
    6 min read
  • Bash Scripting - Working of Alias
    BASH Alias is a shortcut to run commands using some mapping. It is a way to create some shortcut commands for repetitive and multiple tasks. We create an alias in a BASH environment in specified files. We can also incorporate BASH functions, variables, etc to make the Alias more programmatic and fle
    7 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
  • Batch Script - Arrays
    An array is a collection of elements of the same data type. The arrays are not explicitly defined as Batch Script types but can be used. The following items need to be noted when the same members are used in Batch Script. Each aspect of the same members needs to be defined by a set order.A ‘for’ loo
    5 min read
  • Batch Script - Strings
    A Bash script is a plain text file. This file contains different commands for step-by-step execution. These commands can be written directly into the command line but from a re-usability perceptive it is useful to store all of the inter-related commands for a specific task in a single file. We can u
    4 min read
  • Batch Script - Replace a String
    In this article, we are going to Replace a substring with any given string. Batch Script :@echo off set str=GFG is the platform for geeks. echo %str% set str=%str:the=best% echo %str% pause In the above example, we are going to replace 'the' by substring 'best' using %str:the=best% statement. Explan
    1 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
  • Bash Scripting - Bash Echo Command
    In this article, we are going to see the echo command. The Echo command is a built-in command feature for Unix / Linux which is generally used to display the text or message on the screen. Syntax : $ echo [option][text]For Example : $ echo Geeks For GeeksOutput : Geeks For Geeks There are generally
    2 min read
  • Bash Scripting - For Loop
    Since BASH is a command-line language, we get some pretty feature-rich experience to leverage the programming skills to perform tasks in the terminal. We can use loops and conditional statements in BASH scripts to perform some repetitive and tricky problems in a simple programmatic way. In this arti
    5 min read
  • Batch Script - toInt
    In the Batch script, every variable is considered as a string and optionally integers. We need numbers all the time especially when it comes to performing system operations. We might extract numbers from a string variable but performing operations on them are not the same and require some additional
    3 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