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 /R /X /C:"[0-9][0-9\.]*[0-9]  *[0-9][0-9\.]*[0-9]" >NUL || GOTO Syntax
  6.  
  7. SETLOCAL ENABLEDELAYEDEXPANSION
  8.  
  9. :: Initialize variables
  10. SET Compare=0
  11. SET Ver1=%~1
  12. SET Ver2=%~2
  13.  
  14. :: Display debugging info
  15. IF "%DEBUG%"=="1" SET Ver
  16.  
  17. :: Number of digits per version string
  18. SET Digits1=0
  19. SET Digits2=0
  20. FOR %%A IN (%Ver1:.= %) DO SET /A Digits1 += 1
  21. FOR %%A IN (%Ver2:.= %) DO SET /A Digits2 += 1
  22.  
  23. :: Determine lowest and highest number of digits
  24. IF %Digits2% GTR %Digits1% (SET /A MaxDigits=%Digits2%) ELSE (SET /A MaxDigits=%Digits1%)
  25. IF %Digits2% GTR %Digits1% (SET /A MinDigits=%Digits1%) ELSE (SET /A MinDigits=%Digits2%)
  26.  
  27. :: Retrieve individual digits for version string 1
  28. CALL :StoreDigitsInArray DigitValue1 %Ver1:.= %
  29. FOR /F %%A IN ('SET DigitValue1 ^| FIND /C "DigitValue1."') DO SET Digits1=%%A
  30.  
  31. :: Retrieve individual digits for version string 2
  32. CALL :StoreDigitsInArray DigitValue2 %Ver2:.= %
  33. FOR /F %%A IN ('SET DigitValue2 ^| FIND /C "DigitValue2."') DO SET Digits2=%%A
  34.  
  35. :: Display debugging info
  36. IF "%DEBUG%"=="1" SET Digits
  37. IF "%DEBUG%"=="1" SET MaxDigits
  38. IF "%DEBUG%"=="1" SET MinDigits
  39.  
  40. :: Compare digits, break on first difference
  41. FOR /L %%A IN (1,1,%MinDigits%) DO (
  42. 	IF !Compare! EQU 0 (
  43. 		IF !DigitValue1.%%A! GTR !DigitValue2.%%A! SET Compare=2
  44. 		IF !DigitValue1.%%A! LSS !DigitValue2.%%A! SET Compare=-2
  45. 	)
  46. )
  47.  
  48. :: If inconclusive, check the last digits of the longest version string
  49. SET /A MinDigits+=1
  50. FOR /L %%A IN (!MinDigits!,1,%MaxDigits%) DO (
  51. 	IF !Compare! EQU 0 (
  52. 		IF "!DigitValue2.%%A!"=="" IF NOT "!DigitValue1.%%A!"=="0" SET Compare=1
  53. 		IF "!DigitValue1.%%A!"=="" IF NOT "!DigitValue2.%%A!"=="0" SET Compare=-1
  54. 	)
  55. )
  56.  
  57. :: Display debugging info
  58. IF "%DEBUG%"=="1" ECHO CompareVersions=%Compare%
  59.  
  60. :: Return result and quit
  61. ENDLOCAL & SET CompareVersions=%Compare%
  62. GOTO:EOF
  63.  
  64.  
  65. :StoreDigitsInArray
  66. :: %1 is the NAME of the array variable that will contain the individual digits in %2 .. %9
  67. :: Leading zeroes must be removed to prevent the digits from being interpreted as OCTAL numbers.
  68. :: E.g. 3.0.070 would otherwise be interpreted as 3.0.56 in the versions comparison,
  69. :: by removing leading zeroes 3.0.070 will be interpreted as 3.0.70 instead;
  70. :: 3.0.080 would make the batch file fail because 080 is in INVALID octal number,
  71. :: by removing leading zeroes it will be correctly interpreted as 3.0.80.
  72. ::
  73. :: Digit 1
  74. IF "%~1"=="" GOTO:EOF
  75. SET %~1.1=%2
  76. CALL :RemoveLeadingZeroes %~1.1
  77. :: Digit 2
  78. IF "%~2"=="" GOTO:EOF
  79. SET %~1.2=%3
  80. CALL :RemoveLeadingZeroes %~1.2
  81. :: Digit 3
  82. IF "%~3"=="" GOTO:EOF
  83. SET %~1.3=%4
  84. CALL :RemoveLeadingZeroes %~1.3
  85. :: Digit 4
  86. IF "%~4"=="" GOTO:EOF
  87. SET %~1.4=%5
  88. CALL :RemoveLeadingZeroes %~1.4
  89. :: Digit 5
  90. IF "%~5"=="" GOTO:EOF
  91. SET %~1.5=%6
  92. CALL :RemoveLeadingZeroes %~1.5
  93. :: Digit 6
  94. IF "%~6"=="" GOTO:EOF
  95. SET %~1.6=%7
  96. CALL :RemoveLeadingZeroes %~1.6
  97. :: Digit 7
  98. IF "%~7"=="" GOTO:EOF
  99. SET %~1.7=%8
  100. CALL :RemoveLeadingZeroes %~1.7
  101. :: Digit 8
  102. IF "%~8"=="" GOTO:EOF
  103. SET %~1.8=%9
  104. CALL :RemoveLeadingZeroes %~1.8
  105. GOTO:EOF
  106.  
  107.  
  108. :RemoveLeadingZeroes
  109. :: %1 is the NAME of the variable whose VALUE should be stripped of leading zeroes
  110. SET Value=!%1!
  111. :Loop
  112. IF "%Value%"=="0" (
  113. 	SET Value=
  114. 	GOTO:EOF
  115. )
  116. IF "%Value:~0,1%"=="0" (
  117. 	SET /A Value=%Value:~1%
  118. 	SET %1=%Value%
  119. ) ELSE (
  120. 	SET Value=
  121. 	GOTO:EOF
  122. )
  123. GOTO:Loop
  124.  
  125.  
  126. :Syntax
  127. ECHO CompareVersions.bat,  Version 1.02
  128. ECHO Compare 2 version strings
  129. ECHO.
  130. ECHO Usage:     COMPAREVERSIONS  version1  version2
  131. ECHO.
  132. ECHO Notes:     Versions must contain up to eight numerical digits only,
  133. ECHO            separated by dots, e.g. 12.0.1650.1
  134. ECHO            The result is returned in environment variable %%CompareVersions%%.
  135. ECHO            A positive return value means version1 is higher than version2,
  136. ECHO            a negative value means version1 is lower than version2,
  137. ECHO            zero means the versions are equal.
  138. ECHO            If the absolute value of the result equals 2, only the number
  139. ECHO            of digits from the shortest version were compared;
  140. ECHO            if the absolute value equals 1, a number of zeroes had to be
  141. ECHO            applied to the shortest version to make the comparison conclusive.
  142. ECHO.
  143. ECHO Examples:  CompareVersions  10.2.0       10.1.1900         ^(returns  2^)
  144. ECHO            CompareVersions  10.1.0.1900  10.1              ^(returns  1^)
  145. ECHO            CompareVersions  10.00.0.0.0  10.0              ^(returns  0^)
  146. ECHO            CompareVersions  10.0         10.0.0.1850       ^(returns -1^)
  147. ECHO            CompareVersions  9.9.99.9999  10.1              ^(returns -2^)
  148. ECHO.
  149. ECHO Written by Rob van der Woude
  150. ECHO http://www.robvanderwoude.com
  151.  
  152. EXIT /B 1
  153.  

page last uploaded: 2017-08-21, 14:26