Batch File Programming Operators


In this tutorial, you will learn in-depth about different Batch file programming operators, types of operators, their precedence along with respective examples.

Batch File: Unary Operators
Batch File: Arithmetic Operators
Batch File: Relational Operators
Batch File: Logical Shift and Redirection Operator
Batch File: Bitwise Operators
Batch File: Assignment Operators

Batch File Operators

batch file programming operators

Batch file programming supports various types operators for performing mathematical and logical operations similar to other programming languages.

Don’t worry we will explain each batch file programming operators in detail: 

Batch File Programming: Unary Operators


Following list out the unary operator used in batch file programming.

Operators Description
~ Tilde operator
! Boolean NOT operator (negation)
unary minus operator

Tilde (~) is a unary operator used for shortening the long directory names.

Example:

C:\> cd C:\Users\ICT\MicrosoftEdgeBackups\backups\MicrosoftEdgeBackup20180303
is same as
C:\> cd C:\Users\ICT\Micros~1\backups\Micros~1

Both of the above commands do same thing that is just a path to the location “C:\Users\ICT\MicrosoftEdgeBackups\backups\MicrosoftEdgeBackup20180303” on my computer.

Instead of typing all the characters, we can use tilde ~ operator after six consecutive characters to shorten the length.

Note: We have to write 1 after ~ i.e. micros~1

Arithmetic Operators


Batch file programming supports all the normal arithmetic operators as follow:

Operators Description Example
+ Addition operator Set /A 5+5
10
Subtraction operator Set /A 10-5
5
% Modulus operator Set /A 5%2
1
/ Division operator Set /A 20/2
10
* Multiplication operator Set /A 2*2
4

Example

@echo off
SET /A a = 2
SET /A b = 4
Set /A s = %a% + %b%
echo Sum = %s%
pause

Output

Sum = 6

Precedence of Arithmetic operators

The precedence of arithmetic operators are in the following order:

/  *  %  +  -

Let’s clear this from the following example:

@echo off
Set /A s = (20 - 5) * 2 + 8 / 2
echo s = %s%
pause

Output

s = 34

Explanation

The expression enclosed inside ( ) gets the highest priority in the precedence.

In the above example, (20 - 5) gets the highest priority and calculated first giving 15.

Then division operator / gets next priority thus 8 / 2 is evaluated giving 4.

After this multiplication operator * gets next priority thus 15 * 2 yields 30.

Finally, addition operator + performs addition operation 30 + 4 giving final result 34.

Relational Operators


Batch file programming offers relational operators for comparison similar to other programming languages.

Operators Description Example
EQU equal to x EQU y
Return true if x is equal to y
NEQ not equal to x NEQ y
Returns true if x is not equal to y
LSS less than x LSS y
Returns true if x is less than y
LEQ less than or equal to x LEQ y
Returns true if x is less than or equal to y
GTR greater than x GTR y
Returns true if x is greater than y
GEQ greater than or equal to x GEQ y
Returns true if x is greater than or equal to y

Let’s take a look at the example:

@echo off
SET /A x = 3
SET /A y = 4
if %x% EQU %y% echo x is equal to y
if %x% NEQ %y% echo x is not equal to y
if %x% LSS %y% echo x is less than y
if %x% LEQ %y% echo x is less than or equal to y
if %x% GTR %y% echo x is greater than y
if %x% GEQ %y% echo x is greater than or equal to y
pause

Output

batch programming relational operator

Since x is less than y, the above example shows less than results only.

Logical shift and re directional Operators


Operators Description Example
>> Logical shift operator command > filename
Append the output to a file
> Re directional operator command > filename
Redirect the output to a file
< Re directional operator command < filename
Redirect text to command

Redirection operator is used to redirecting the output of one command to the other file.

Example 1: > operator

@echo off
echo Testing of redirection operator > test.txt

The above command will redirect the output of echo command 'Testing of redirection operator' to a text file named test.txt.

If there is no text file named test it will create a new one.

Example 2: > operator

@echo off
NET USER > test.txt

This will print the output of NET USER command in test.txt file as shown below:

User accounts for \\ICT

-----------------------------------------------------
Administrator            DefaultAccount           Guest                    
ICT                      WDAGUtilityAccount       
The command completed successfully.

Example 3:

@echo off
ping google.com >> test.txt

Now the above code will append the output of ping command to the test.txt file which was created earlier.

Output


User accounts for \\ICT

-----------------------------------------------
Administrator            DefaultAccount           Guest                    
ICT                      WDAGUtilityAccount       
The command completed successfully.


Pinging google.com [216.58.221.46] with 32 bytes of data:
Reply from 216.58.221.46: bytes=32 time=59ms TTL=53
Request timed out.
Reply from 216.58.221.46: bytes=32 time=59ms TTL=53
Reply from 216.58.221.46: bytes=32 time=64ms TTL=53

Ping statistics for 216.58.221.46:
    Packets: Sent = 4, Received = 3, Lost = 1 (25% loss),
Approximate round trip times in milli-seconds:
    Minimum = 59ms, Maximum = 64ms, Average = 60ms

Example 4: Using < operator
I have created a file named Num.txt which contains numbers in random order as follow:

4
3
7
1
8

Now let’s run following code and see the output.

@echo off
SORT < Num.txt
pause

Output

batch redirection operator

Explanation

The above example sorts the random numbers of Num.txt file and displays in the screen.

Bitwise Operators


Let’s look at the bitwise operators in a batch script.

Operators Description
& Bitwise and operator
| Bitwise or operator
^ Bitwise exclusive or operator

Assignment Operators


Following are the list of assignment operators in the batch file programming.

Operators Example Description
+= Set /A x = 4
x += 2
Value of x: 6
2 is added to the value of x and
the result is assigned to x
-= Set /A x = 4
x -= 2
Value of x: 2
2 is subtracted from the value of x
and the result is assigned to x
*= Set /A x = 4
x *= 2
Value of x: 8
2 is multiplied to the value of x
and the result is assigned to x
/= Set /A x = 4
x /= 2
Value of x: 2
The value of x is divided by 2
and the result is assigned to x
%= Set /A x = 4
x %= 2
Value of x: 2
This will find the modulus
and result is assigned to x

Let’s clear it out through example:

@echo off
SET /A x = 4
SET /A x += 2
echo x = %x%
SET /A x -= 2
echo x = %x%
SET /A x *= 2
echo x = %x%
SET /A x /= 2
echo x = %x%
SET /A x %= 2
echo x = %x%
pause

Output

batch file assignment operator

Remaining Batch File Programming Operators


Operators Description
, Separator
&& For using Multiple Commands
|| For executing one from many commands

Example: Batch && Operator

&& operator is used for executing multiple commands is a single line.

@echo off
SET /A x = 4 && SET /A x += 2
echo x = %x%
pause

Output

x = 6

Explanation

In the above example, both SET commands are executed in a single line.