Srijani Ghosh
Srijani Ghosh

Reputation: 4216

Unable to compare 2 integer variables in batch file

I have this following piece of code where I am basically trying to compare 2 integers in a batch file (one of them is retrieved from registry) -

set mainCounter=1
reg add HKLM\Software\Looptest /f /v mainCounter1 /t REG_SZ /d 0

rem ---- some code here ---

FOR /f "tokens=2*" %%a in ('reg query "HKLM\SOFTWARE\Looptest" /v "mainCounter1"') do set "mainCounter1=%%b"
echo mainCounter :: %mainCounter%
echo mainCounter1 :: %mainCounter1%

if %mainCounter% EQU %mainCounter1% goto _reImageSystem

:_reImageSystem
echo mainCounter while reimaging :: %mainCounter%
echo mainCounter1 while reimaging :: %mainCounter1%
pause

This code clearly suggests that, value of mainCounter is 1 (set manually) And, The value of mainCounter1 is set to registry.. which is 0. Still , when I compare if %mainCounter% EQU %mainCounter1% goto _reImageSystem , clearly it should not go inside _reImageSystem, but it goes...

The output is:

mainCounter :: 1
mainCounter1 :: 0
mainCounter while reimaging :: 1
mainCounter1 while reimaging :: 0

So, possible the comparison is not working..

if %mainCounter% EQU %mainCounter1% goto _reImageSystem

Any suggestions ?

Thanks!

Upvotes: 1

Views: 2604

Answers (1)

Alex K.
Alex K.

Reputation: 175776

If the comparison is false it will still fall through to the label and execute.

To prevent this branch away:

if %mainCounter% EQU %mainCounter1% goto _reImageSystem
goto end

:_reImageSystem
bla bla
bla bla

:end

(Or GOTO:eof to end or invert your logic and use NEQ)

Upvotes: 1

Related Questions