Batch Script - Logical Operators


Advertisements

Logical operators are used to evaluate Boolean expressions. Following are the logical operators available.

The batch language is equipped with a full set of Boolean logic operators like AND, OR, XOR, but only for binary numbers. Neither are there any values for TRUE or FALSE. The only logical operator available for conditions is the NOT operator.

The easiest way to implement the AND/OR operator for non-binary numbers is to use the nested IF condition. The following example shows how this can be implemented.

Example

@echo off
SET /A a = 5
SET /A b = 10
IF %a% LSS 10 (IF %b% GTR 0 (ECHO %a% is less than 10 AND %b% is greater than 0))

Output

The above command produces the following output.

5 is less than 10 AND 10 is greater than 0

Following is an example of the AND operation that can be implemented using the IF statement.

Example

@echo off
SET /A a = 5
SET /A b = 10

IF %a% GEQ 10 (
   IF %b% LEQ 0 (
      ECHO %a% is NOT less than 10 OR %b% is NOT greater than 0
   ) ELSE (
      ECHO %a% is less than 10 OR %b% is greater than 0
   )
) ELSE (
   ECHO %a% is less than 10 OR %b% is greater than 0
)

Output

The above command produces the following output.

5 is less than 10 AND 10 is greater than 0

Following is an example of how the NOT operator can be used.

Example

@echo off
SET /A a = 5
IF NOT %a%==6 echo "A is not equal to 6"

Output

The above command produces the following output.

"A is equal to 5"
batch_script_operators.htm
Advertisements