Rob van der Woude's Scripting Pages

Boolean Logic in Batch Files

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

This page will teach you how to emulate the missing logical operators.

The basics: IF

To test if a condition is true, IF is used:

IF EXIST filename ...
IF %value% LSS 10 ...
IF /I NOT "%string1%"=="string2" ...
IF NOT ERRORLEVEL 1 ...

Unfortunately, there is no such thing as IF %1 LSS 10 AND %2 GTR 0 ... so we'll have to emulate the missing operators.

The AND operator

The AND operator is the easiest one to emulate: we can just "nest" IF statements.

IF %1 LSS 10 (
	IF %2 GTR 0 (
		ECHO %%1 is less than 10 AND %%2 is greater than 0
	)
)

or:

IF %1 LSS 10 (IF %2 GTR 0 (ECHO %%1 is less than 10 AND %%2 is greater than 0))

or:

IF %1 LSS 10 IF %2 GTR 0 ECHO %%1 is less than 10 AND %%2 is greater than 0

Even though the three code snippets are identical, we will use the first one for better readability, especially when the equation becomes more complex.

An example: if %1 must be less than 10 AND %2 must be greater than 0, and we want to include an ELSE statement in case these conditions are not both true, the need for parenthesis becomes obvious.

Consider these two code snippets:

IF %1 LSS 10 (
	IF %2 GTR 0 (
		ECHO %1 is less than 10 AND %2 is greater than 0
	)
) ELSE (
	ECHO %1 is NOT less than 10
)

and

IF %1 LSS 10 (
	IF %2 GTR 0 (
		ECHO %1 is less than 10 AND %2 is greater than 0
	) ELSE (
		ECHO %1 is NOT less than 10 OR %2 is NOT greater than 0
	)
)

The placement of the ELSE statement is critical!

The code snippet above still does not cover the case when %1 is greater than or equal to 10.
To complete it, we would need:

IF %1 LSS 10 (
	IF %2 GTR 0 (
		ECHO %1 is less than 10 AND %2 is greater than 0
	) ELSE (
		ECHO %1 is NOT less than 10 OR %2 is NOT greater than 0
	)
) ELSE (
	ECHO %1 is NOT less than 10 OR %2 is NOT greater than 0
)

Unlike the simple basic example, to convert these code snippets to one-liners again is impossible without parenthesis.
You may often be tempted to remove some parenthesis, but this may change the logic of the equation.

The OR operator

Often the OR operator can be replaced by AND and NOT operaters: if condition 1 OR condition 2 must be true, you may also say that NOT condition 1 AND NOT condition must be false.
An example: we want to replace the AND operator in the previous example by the OR operator, i.e. %1 must be less than 10 OR %2 must be greater than 0.
For numerical comparisons we don't need NOT but we can replace LSS by GEQ etcetera.
We might try:

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

The XOR operator

Though possible, emulating the XOR operator with the techniques described before would require far too complex code snippets, a debugger's nightmare.

Alternatives

There are two alternative techniques:

  1. use temporary variables for the result
  2. use temporary variables per condition, combined with binary math

Temporary variables for the result

This technique is perfect for both the AND and the OR operator.
OR:

SET Result=0
IF %1 LSS 10 SET Result=1
IF %2 GTR  0 SET Result=1
IF %Result% EQU 1 (
	ECHO %1 is less than 10 OR %2 is greater than 0
) ELSE (
	ECHO %1 is NOT less than 10 AND %2 is NOT greater than 0
)

AND:

SET Result=1
IF NOT %1 LSS 10 SET Result=0
IF NOT %2 GTR  0 SET Result=0
IF %Result% EQU 1 (
	ECHO %1 is less than 10 AND %2 is greater than 0
) ELSE (
	ECHO %1 is NOT less than 10 OR %2 is NOT greater than 0
)
Note: In the example above, IF NOT %1 LSS 10 ... is used to explain the technique.
In real life, you would probably use IF %1 GEQ 10 ...

Consider using 4 conditions, and compare the complexity of the techniques described so far.

To emulate XOR for 2 conditions you can use SET Result += 1 and IF %Result% EQU 1 instead.

Temporary variables per condition, combined with binary math

For this technique, use a (binary) variable for each condition:

IF %1 LSS 10 (SET Cond1=1) ELSE (SET Cond1=0)
IF %2 GTR  0 (SET Cond2=1) ELSE (SET Cond2=0)

Next, use binary math to get the results:

SET /A "ResultAND = %Cond1% & %Cond2%"
IF %ResultAND% EQU 1 (ECHO AND: TRUE) ELSE (ECHO AND: FALSE)

SET /A "ResultOR = %Cond1% | %Cond2%"
IF %ResultOR% EQU 1 (ECHO OR:  TRUE) ELSE (ECHO OR:  FALSE)

SET /A "ResultXOR = %Cond1% ˆ %Cond2%"
IF %ResultXOR% EQU 1 (ECHO XOR: TRUE) ELSE (ECHO XOR: FALSE)

Summary

We have discussed techniques for binary logic in batch files.
Though there is a full set of operators available for binary math, they are lacking for logical conditions.
This page shows how to translate these conditions to 0 (FALSE) or 1 (TRUE) and use binary math to get the logical results.


page last modified: 2011-03-04; loaded in 0.0013 seconds