Skip to main content

Batch Script Logical operators

Logical operators

Logical operators are used to evaluate Boolean expressions.

The batch language has a full set of Boolean logical operators such as AND, OR, XOR, but only for binary numbers. There are also no values for TRUE or FALSE.

note

The only logical operator available for conditions is the NOT operator.

So, if statements do not support logical operators.

The following table lists the available logical operators.

OperatorDescription
ANDThis is the logical and operator
ORThis is the logical or operator
NOTThis is the logical not operator

Implement AND operator to use in if conditions

Logical AND is implemented with nested conditions:

"AND
@echo off
set /A a=5
set /A b=10
if %a% geq 5 (
if %b% leq 10 (
echo "True Result"
)
)
"True Result"

Implement OR operator to use in if conditions

Logical OR is implemented with a separate variable:

OR implementation
@echo off
set /A a=5
set /A b=10
set result=false
if %a% == 1 set result=true
if %b% == 1 set result=true
if "%result%" == "true" (
echo "True result"
)
"True Result"
note

You are essentially using an additional variable to accumalate your boolean result over multiple if statements.