Search code examples
batch-filecmddos

Windows Batch GEQ not working as expected


I am trying to use GEQ (Greater than, or equal) in a Windows Batch script. I have an IF clause testing for

if %count% GEQ 10

while counting %count% one up each pass. I had expected the condition to be true after ten passes. However, after running two times, the condition is true, while %count% is 2.

If I replace GEQ with ==, it works as expected, displays the number of passes up to ten and quits. I really don't understand where my error in thinking is, at this point. Please give me a heads-up.

Code:

@ECHO OFF
SETLOCAL
CLS

ECHO.
SET /a max = 10
ECHO This should run %max% times:

:REPEAT
set /a count = "%count%"+1
ECHO Test %count%

IF "%count%" GEQ "%max%" (
    GOTO :QUIT
) ELSE (
    GOTO :REPEAT
)

:QUIT
ECHO.
ECHO Done.

Solution

  • IF "%count%" GEQ "%max%" (
    

    by using the quotes, you force if to compare strings, not numbers, so 2 is bigger than 19514

    Instead write:

    IF %count% GEQ %max% (