Mikoyan
Mikoyan

Reputation: 59

Unable to compare decimal number via batch

Having an issue whereby I cannot seem to make the following script work. Basically as a summary I need to ensure a version of an application is greater than 2.0.0.8

So here is my code:

setlocal enabledelayedexpansion
set n2=2.0.0.8
FOR /F "tokens=2* delims=0   " %%A IN ('REG QUERY "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Uninstall
\FB58E75BC1D58DF60340894DB00D3FA9BBD513B6" /v displayversion') DO SET n1=%%B 
IF %n1% GEQ %n2% (goto success) else (goto error)

:success
start notepad.exe
Goto:eof

:error
start winword.exe
Goto:eof

:eof
exit

The output works fine and n1 outputs a version greater than 2.0.0.8 (it output 2.0.0.12) using notepad and winword launching purely as testing.

No matter what it always errors and opens Winword.exe

The value in testing is 2.0.0.12 so why is this still erroring out?

Hope this makes sense

Thanks

Mikoyan

Upvotes: 0

Views: 166

Answers (2)

Mikoyan
Mikoyan

Reputation: 59

setlocal enabledelayedexpansion
set n2=2.0.0.8
FOR /F "tokens=2* delims=0   " %%A IN ('REG QUERY "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Uninstall\FB58E75BC1D58DF60340894DB00D3FA9BBD513B6" /v displayversion') DO SET n1=%%B 
for /f "tokens=1,2,3,4 delims=." %%a in ("%n2%") do set "v2=%%a%%b%%c%%d"
for /f "tokens=1,2,3,4 delims=." %%a in ("%n1%") do set "v1=%%a%%b%%c%%d"
IF %v1% GEQ %v2% (goto success) else (goto error)

Upvotes: 0

npocmaka
npocmaka

Reputation: 57252

Try to remove the dots so you'll compare them as decimals:

setlocal enabledelayedexpansion
set n2=2.0.0.8
FOR /F "tokens=2* delims=0   " %%A IN (
    'REG QUERY "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Uninstall\FB58E75BC1D58DF60340894DB00D3FA9BBD513B6" /v displayversion'
) DO SET n1=%%B 

for /f "tokens=1,2,3,4 delims=." %%a in ("%n2%") do set "v2=%%a%%b%%c%%d"
for /f "tokens=1,2,3,4 delims=." %%a in ("%n1%") do set "v1=%%a%%b%%c%%d"

IF %v1% GEQ %v2% (goto success) else (goto error)

Upvotes: 2

Related Questions