Basics of Batch Scripting
Last Updated :
29 Sep, 2022
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:-
- Create a new text file with a ‘.txt‘ extension.
- Now rename this file with extension as ‘.bat‘ this creates a Batch file.
- 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.

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%

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%

Demonstration of all arithmetic operators
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
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 - 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 - 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
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 - 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 - 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
Bash Script - Working of Bash Variables
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 reusability perceptive it is useful to store all of the inter-related commands for a specific task in a single file. We can us
7 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