Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for compareversions.bat

(view source code of compareversions.bat as plain text)

  1. @ECHO OFF
  2. :: Check command line arguments
  3. IF     "%~2"=="" GOTO Syntax
  4. IF NOT "%~3"=="" GOTO Syntax
  5. ECHO.%*| FINDSTR.EXE /R /X /C:"[0-9][0-9\.]*[0-9]  *[0-9][0-9\.]*[0-9]" >NUL || GOTO Syntax
  6.  
  7. SETLOCAL ENABLEDELAYEDEXPANSION
  8.  
  9. SET Ver1=%1.
  10. :: Remove up to 40 leading zeroes
  11. FOR /L %%A IN (1,1,40) DO SET Ver1=!Ver1:.0=.!
  12. :: Restore wiped out zero values
  13. SET Ver1=%Ver1:..=.0.%
  14. :: Replace dots by spaces
  15. SET Ver1=%Ver1:.= %
  16. :: Convert version string to array of digits
  17. CALL :ToArray Version1 %Ver1%
  18. :: Count number of digits (add 1 to simplify ToArray code)
  19. SET Digits1=1
  20. FOR /F %%A IN ('SET Version1') DO SET /A Digits1 += 1
  21.  
  22. SET Ver2=%2.
  23. FOR /L %%A IN (1,1,40) DO SET Ver2=!Ver2:.0=.!
  24. SET Ver2=%Ver2:..=.0.%
  25. SET Ver2=%Ver2:.= %
  26. CALL :ToArray Version2 %Ver2%
  27. SET Digits2=1
  28. FOR /F %%A IN ('SET Version2') DO SET /A Digits2 += 1
  29.  
  30. :: Count number of digits to compare
  31. IF %Digits1% GTR %Digits2% (SET Max=%Digits1%) ELSE (SET Max=%Digits2%)
  32.  
  33. :: Compare digits one by one until we have a "winner"
  34. SET Highest=0
  35. FOR /L %%A IN (2,1,%Max%) DO (
  36. 	IF !Highest! EQU 0 (
  37. 		IF !Version1.%%A!0 GTR !Version2.%%A!0 (
  38. 			SET Highest=1
  39. 		) ELSE IF !Version2.%%A!0 GTR !Version1.%%A!0 (
  40. 			SET Highest=2
  41. 		)
  42. 	)
  43. )
  44.  
  45. IF %Highest% EQU 0 (
  46. 	ECHO Versions are equal
  47. 	ENDLOCAL && SET CompareVersion=%1
  48. 	EXIT /B 0
  49. )
  50.  
  51. CALL SET CompareVersion=%%!Highest!
  52. ECHO Highest version is %CompareVersion%
  53. :: Use a "one-liner" to keep variable values despite ENDLOCAL
  54. ENDLOCAL & (SET CompareVersion=%CompareVersion%) & EXIT /B %Highest%
  55.  
  56.  
  57. :ToArray
  58. :: %1 is the array variable NAME
  59. :: %2..%9 is the version string, separated by spaces instead of dots
  60. SET Ver=%2
  61. SET Ver=%Ver:.= %
  62. FOR /L %%A IN (2,1,9) DO (
  63. 	CALL SET /A %1.%%A=%%%%A 2>NUL
  64. )
  65. GOTO:EOF
  66.  
  67.  
  68. :Syntax
  69. ECHO.
  70. ECHO CompareVersions.bat,  Version 2.01
  71. ECHO Compare 2 version strings
  72. ECHO.
  73. ECHO Usage:     ←[32mCOMPAREVERSIONS  version1  version2←[0m
  74. ECHO.
  75. ECHO Notes:     Versions can contain up to eight numerical only digits,
  76. ECHO            separated by dots, e.g. 12.0.1650.1
  77. ECHO            If versions have different number of digits, the "missing"
  78. ECHO            digits of the shortest one will be assumed 0.
  79. ECHO            Leading zeroes are ignored.
  80. ECHO            The result is returned in environment variable
  81. ECHO            ←[32m%%CompareVersions%%←[0m and displayed on screen.
  82. ECHO            Return value (ErrorLevel) equals 1 if version1 is higher
  83. ECHO            than version2, 2 if version2 is higher than version1, 0 if
  84. ECHO            versions are equal, or -1 in case of (command line) errors.
  85. ECHO.
  86. ECHO Examples:  CompareVersions  10.00.0.0.0  10.0          (ErrorLevel 0)
  87. ECHO            CompareVersions  10.1.0.1900  10.1          (ErrorLevel 1)
  88. ECHO            CompareVersions  10.0         10.0.0.1850   (ErrorLevel 2)
  89. ECHO.
  90. ECHO Written by Rob van der Woude
  91. ECHO https://www.robvanderwoude.com
  92.  
  93. EXIT /B -1

page last modified: 2024-04-16; loaded in 0.0085 seconds