(view source code of compareversions.bat as plain text)
@ECHO OFF
:: Check command line arguments
IF "%~2"=="" GOTO Syntax
IF NOT "%~3"=="" GOTO Syntax
ECHO.%*| FINDSTR.EXE /R /X /C:"[0-9][0-9\.]*[0-9] *[0-9][0-9\.]*[0-9]" >NUL || GOTO Syntax
SETLOCAL ENABLEDELAYEDEXPANSION
SET Ver1=%1.
:: Remove up to 40 leading zeroes
FOR /L %%A IN (1,1,40) DO SET Ver1=!Ver1:.0=.!
:: Restore wiped out zero values
SET Ver1=%Ver1:..=.0.%
:: Replace dots by spaces
SET Ver1=%Ver1:.= %
:: Convert version string to array of digits
CALL :ToArray Version1 %Ver1%
:: Count number of digits (add 1 to simplify ToArray code)
SET Digits1=1
FOR /F %%A IN ('SET Version1') DO SET /A Digits1 += 1
SET Ver2=%2.
FOR /L %%A IN (1,1,40) DO SET Ver2=!Ver2:.0=.!
SET Ver2=%Ver2:..=.0.%
SET Ver2=%Ver2:.= %
CALL :ToArray Version2 %Ver2%
SET Digits2=1
FOR /F %%A IN ('SET Version2') DO SET /A Digits2 += 1
:: Count number of digits to compare
IF %Digits1% GTR %Digits2% (SET Max=%Digits1%) ELSE (SET Max=%Digits2%)
:: Compare digits one by one until we have a "winner"
SET Highest=0
FOR /L %%A IN (2,1,%Max%) DO (
IF !Highest! EQU 0 (
IF !Version1.%%A!0 GTR !Version2.%%A!0 (
SET Highest=1
) ELSE IF !Version2.%%A!0 GTR !Version1.%%A!0 (
SET Highest=2
)
)
)
IF %Highest% EQU 0 (
ECHO Versions are equal
ENDLOCAL && SET CompareVersion=%1
EXIT /B 0
)
CALL SET CompareVersion=%%!Highest!
ECHO Highest version is %CompareVersion%
:: Use a "one-liner" to keep variable values despite ENDLOCAL
ENDLOCAL & (SET CompareVersion=%CompareVersion%) & EXIT /B %Highest%
:ToArray
:: %1 is the array variable NAME
:: %2..%9 is the version string, separated by spaces instead of dots
SET Ver=%2
SET Ver=%Ver:.= %
FOR /L %%A IN (2,1,9) DO (
CALL SET /A %1.%%A=%%%%A 2>NUL
)
GOTO:EOF
:Syntax
ECHO.
ECHO CompareVersions.bat, Version 2.01
ECHO Compare 2 version strings
ECHO.
ECHO Usage: ←[32mCOMPAREVERSIONS version1 version2←[0m
ECHO.
ECHO Notes: Versions can contain up to eight numerical only digits,
ECHO separated by dots, e.g. 12.0.1650.1
ECHO If versions have different number of digits, the "missing"
ECHO digits of the shortest one will be assumed 0.
ECHO Leading zeroes are ignored.
ECHO The result is returned in environment variable
ECHO ←[32m%%CompareVersions%%←[0m and displayed on screen.
ECHO Return value (ErrorLevel) equals 1 if version1 is higher
ECHO than version2, 2 if version2 is higher than version1, 0 if
ECHO versions are equal, or -1 in case of (command line) errors.
ECHO.
ECHO Examples: CompareVersions 10.00.0.0.0 10.0 (ErrorLevel 0)
ECHO CompareVersions 10.1.0.1900 10.1 (ErrorLevel 1)
ECHO CompareVersions 10.0 10.0.0.1850 (ErrorLevel 2)
ECHO.
ECHO Written by Rob van der Woude
ECHO https://www.robvanderwoude.com
EXIT /B -1
page last modified: 2024-04-16; loaded in 0.0085 seconds