Skip to main content

How to Compare Numbers in Batch Script (EQU, NEQ, LSS, LEQ, GTR, GEQ)

Comparing numbers is a fundamental building block of logic in any programming or scripting language. In Windows Batch, you frequently need to check if a counter has reached a limit, if a file size exceeds a threshold, or if a command's exit code signals success or failure. For these tasks, the IF command provides a specific set of operators designed exclusively for numerical comparison.

This guide will teach you the six essential operators for comparing numbers, explain the critical difference between numerical comparison (EQU) and string comparison (==), and show you how to use these operators to create intelligent, conditional logic in your scripts.

The Core Command: The IF Statement for Numbers

The IF statement is the primary tool for conditional logic. While it can compare strings, it has a special set of operators for comparing two values as integers.

The basic syntax is: IF [NOT] value1 OP value2 (command)

  • value1, value2: The numbers or variables you want to compare.
  • OP: The comparison OPerator.

The Six Comparison Operators Explained

These operators are mnemonic and correspond to standard mathematical comparisons.

OperatorMeaningExample
EQUEqual toIF %count% EQU 10
NEQNot Equal toIF %ERRORLEVEL% NEQ 0
GTRGreater ThanIF %size% GTR 1024
LSSLess ThanIF %attempts% LSS 5
GEQGreater than or Equal toIF %score% GEQ 90
LEQLess than or Equal toIF %age% LEQ 18

These operators correctly handle positive and negative integers, as well as different number formats (e.g., 01, 1, and 0x1 for hexadecimal if used in a SET /A context).

Basic Example: A Simple Number Comparison

This script demonstrates the basic usage of these operators.

@ECHO OFF
SET "ValueA=10"
SET "ValueB=20"

ECHO Comparing %ValueA% and %ValueB%...
ECHO.

IF %ValueA% LSS %ValueB% (
ECHO %ValueA% is less than %ValueB%.
)

IF %ValueA% GTR %ValueB% (
ECHO %ValueA% is greater than %ValueB%.
)

IF %ValueA% EQU %ValueA% (
ECHO %ValueA% is equal to %ValueA%.
)

Output:

Comparing 10 and 20...

10 is less than %ValueB%.
10 is equal to 10.

The Crucial Difference: EQU vs. == (Numerical vs. String)

This is the most common point of confusion for new batch scripters.

  • EQU, NEQ, GTR, etc., are for numerical comparisons.
  • == is for string (text) comparisons.

These two types of comparisons can produce very different results, especially with numbers that have leading zeros.

Example

Let's compare the number 10 with the string "010".

@ECHO OFF
ECHO --- Comparing 10 and "010" ---
ECHO.

ECHO Numerical Comparison:
IF 10 EQU 010 (
ECHO 10 EQU 010 is TRUE
) ELSE (
ECHO 10 EQU 010 is FALSE
)

ECHO.
ECHO String Comparison:
IF "10"=="010" (
ECHO "10"=="010" is TRUE
) ELSE (
ECHO "10"=="010" is FALSE
)

Output:

--- Comparing 10 and "010" ---

Numerical Comparison:
10 EQU 010 is TRUE

String Comparison:
"10"=="010" is FALSE

The numerical comparison (EQU) is true because both 10 and 010 represent the same integer value. The string comparison (==) is false because the sequence of characters "10" is different from "010".

Rule of Thumb: If you are working with numbers, always use the letter-based operators (EQU, GTR, etc.).

Common Pitfalls and How to Solve Them

Problem: The Values are Not Numbers

What happens if you try to use a numerical operator on a string that isn't a number?

Example of script with error:

IF "apple" GTR "banana" ( ... )

The IF command will not crash. Instead, it will fall back to a string comparison. This can lead to very confusing results, as "9" GTR "10" would be true because the character '9' comes after the character '1'.

Solution: Ensure your variables contain valid numbers before comparing them. You can use the SET /A trick or FINDSTR to validate them first.

Problem: Using the Wrong Operator

Using == when you mean EQU is the most frequent mistake. It will seem to work for most numbers but will fail with edge cases like leading zeros.

Solution: Be deliberate. When you are comparing numbers, make a conscious choice to use the numerical operators.

6. Practical Example: Checking %ERRORLEVEL%

The most common use case for numerical comparison is checking the exit code of a command.

@ECHO OFF
SETLOCAL

REM --- Run a command that might fail ---
PING a-fake-hostname > NUL 2> NUL

ECHO --- Checking the result ---
ECHO The exit code was: %ERRORLEVEL%

IF %ERRORLEVEL% EQU 0 (
ECHO [SUCCESS] The command completed successfully.
)

IF %ERRORLEVEL% GTR 0 (
ECHO [FAILURE] The command failed with a non-zero exit code.
)

ENDLOCAL

This script robustly checks the outcome of the PING command and reports a clear, human-readable status.

Conclusion

Using the correct operators for numerical comparison is essential for writing accurate and reliable conditional logic in batch scripts.

Key takeaways:

  • There are six operators for numerical comparison: EQU, NEQ, GTR, LSS, GEQ, and LEQ.
  • These operators should be used whenever you are comparing integer values.
  • The double equals sign (==) is for string comparison only. Using it on numbers can lead to incorrect results.
  • A primary use case for these operators is checking the %ERRORLEVEL% after a command to determine if it succeeded (EQU 0) or failed (NEQ 0).