if-statementbatch-filecmdcompareversion

Batch: Comparing version numbers via GEQ when said version uses dots and varying number of digits


I've been using the following bit of CMD code to check Edge versions, but the move from 99 to 100 seems to have thrown a wrench in the code.

FOR /F "Tokens=2*" %%G IN ('REG QUERY "HKLM\SOFTWARE\WOW6432Node\Microsoft\EdgeUpdate\Clients\{56EB18F8-B008-4CBD-B6D2-8C97FE7E9062}" /V "pv" ^| FINDSTR "REG_SZ"') DO (IF %%H GEQ 100.0.1185.29 (ECHO Microsoft Edge is version %%H and does not require an update. & GOTO SomethingElse) ELSE (ECHO Microsoft Edge is version %%H and requires an update. & GOTO MSEdgeInstall))

It now thinks that, for example, 99.9.1150.30 is higher than 100.0.1185.29. Now if I were to remove the dots, it would realise that 999115030 was, in fact, smaller than 1000118529. There's something about the presence of dots and an additional digit that causes the calculation in cmd to fail.

Is there some clever way to resolve this, or do I have to essentially remove the dots from the registry query before comparing from now on?


Solution

  • While Magoo's code errored out for me somewhere, or I'm just not clever enough to understand it, it did give me a good advice on comparing each number individually, so thank you for that Magoo. I'd upvote you but my rank's too low apparently.

    What I came up with instead is the following:

    @ECHO Off
    
    SET "DesiredVersionColumn01=100"
    SET "DesiredVersionColumn02=0"
    SET "DesiredVersionColumn03=1185"
    SET "DesiredVersionColumn04=29"
    
    FOR /F "Tokens=3*" %%G IN ('REG QUERY "HKLM\SOFTWARE\WOW6432Node\Microsoft\EdgeUpdate\Clients\{56EB18F8-B008-4CBD-B6D2-8C97FE7E9062}" /V "pv" ^| FINDSTR "REG_SZ"') DO (SET "CurrentVersionFull=%%G")
    FOR /F "Tokens=1* Delims=. " %%G IN ('ECHO %CurrentVersionFull%') DO (SET "CurrentVersionColumn01=%%G" & SET "CurrentVersionColumnRemaining=%%H")
    FOR /F "Tokens=1* Delims=. " %%G IN ('ECHO %CurrentVersionColumnRemaining%') DO (SET "CurrentVersionColumn02=%%G" & SET "CurrentVersionColumnRemaining=%%H")
    FOR /F "Tokens=1* Delims=. " %%G IN ('ECHO %CurrentVersionColumnRemaining%') DO (SET "CurrentVersionColumn03=%%G" & SET "CurrentVersionColumn04=%%H")
    
    REM These are just for show, not really needed:
    ECHO Edge Full Version: %CurrentVersionFull%
    
    ECHO Edge Version Without Dots: %CurrentVersionColumn01% %CurrentVersionColumn02% %CurrentVersionColumn03% %CurrentVersionColumn04%
    
    REM Version Checking
    ECHO Ensuring that Microsoft Edge is version %DesiredVersionColumn01%.%DesiredVersionColumn02%.%DesiredVersionColumn03%.%DesiredVersionColumn04% or above.
    IF %CurrentVersionColumn01% GTR %DesiredVersionColumn01% (ECHO Microsoft Edge is version %CurrentVersionFull% and does not require an update. C1 & GOTO NoInstall)
    IF %CurrentVersionColumn01% EQU %DesiredVersionColumn01% IF %CurrentVersionColumn02% GTR %DesiredVersionColumn02% (ECHO Microsoft Edge is version %CurrentVersionFull% and does not require an update. C2 & GOTO NoInstall)
    IF %CurrentVersionColumn02% EQU %DesiredVersionColumn02% IF %CurrentVersionColumn03% GTR %DesiredVersionColumn03% (ECHO Microsoft Edge is version %CurrentVersionFull% and does not require an update. C3 & GOTO NoInstall)
    IF %CurrentVersionColumn03% EQU %DesiredVersionColumn03% IF %CurrentVersionColumn04% GEQ %DesiredVersionColumn04% (ECHO Microsoft Edge is version %CurrentVersionFull% and does not require an update. C4 & GOTO NoInstall)
    ECHO ECHO Microsoft Edge is version %CurrentVersionFull% and requires an update. C Full & GOTO Install
    
    
    :Install
    ECHO Installing
    PAUSE
    EXIT
    
    :NoInstall
    ECHO Not Installing
    PAUSE
    EXIT
    

    I think this is now error-proof. If it isn't then I missed it lol.

    I'll still try to find out why Magoo's code is erroring out for me, as it seems more efficient than mine.