Batch Script Operators

An operator is a symbol that specifies an action to perform in an expression, for example, the ‘+’ symbol in the expression ‘1 + 2’ would add the two numbers together to produce a result. This type of operator is known as an arithmetic operator, however, there are also other groups of operators within Batch Script that will be explained below.

Arithmetic Operators

Arithmetic operators allow for mathematical calculations to be carried out within Batch Script.

Operator Description
+ Adds two values together.
Subtracts one value from another.
* Multiplies two values together.
/ Divides one value by another.

The following examples show each of these operators in action. One thing to note here is that Batch Script does not support floating point arithmetic, so, for example, the result of dividing 8 by 3 will be truncated to 2, rather than 2.66666666666667.

:: Variables to use in expressions.
set /A num1 = 3
set /A num2 = 8

:: num3 will equal 11.
set /A num3 = %num1% + %num2%

:: num3 will equal 5.
set /A num3 = %num2% - %num1%

:: num3 will equal 24.
set /A num3 = %num1% * %num2% 

:: num3 will equal 2 (2.66666666666667 truncated to 2).
set /A num3 = %num2% / %num1%

Assignment Operators

Assignment operators in Batch Script are used to assign values to a variable, the most basic of which is ‘=’. Here, the ‘=’ symbol means ‘gets set to’ rather than ‘equals’, so in the expression ‘x = y’, ‘x’ gets set to ‘y’. The arithmetic operator symbols can be combined with ‘=’ to form the assignment operators.

Operator Description
= Assigns the value of the right side operand to the left side operand.
+= Adds the value from the right side operand to the left side operand and assigns the value to the left side operand.
-= Subtracts the value of the right side operand from the left side operand and assigns the value to the left side operand.
*= Multiplies the value from the right side operand with the left side operand and assigns the value to the left side operand.
/= Divides the left side operand by the value of the right side operand and assigns the value to the left side operand.

The examples below show these assignment operators in action.

:: num1 gets set to 10.
set /A num1 = 10

set /A num2 = 2
:: num2 gets set to 7.
set /A num2 += 5

set /A num3 = 10
:: num3 gets set to 6.
set /A num3 -= 4

set /A num4 = 2
:: num4 gets set to 4.
set /A num4 *= 2

set /A num5 = 20
:: num5 gets set to 4.
set /A num5 /= 5

Relational Operators

Relational operators, or comparison operators, as they are sometimes known, compare two values and are used, for example, in ‘if’ statements, which are described in the next section on Decision Making. The result of the comparison is either True or False.

Operator Description
equ Evaluates to True if the two values are equal.
neq Evaluates to True if the two values are not equal.
gtr Evaluates to True if the first value is greater than the second.
lss Evaluates to True if the first value is less than the second.
geq Evaluates to True if the first value is greater than or equal to the second.
leq Evaluates to True if the first value is less than or equal to the second.

It should be noted that these operators can be used to compare string values as well as numbers.