VersionCheckNew: Compare version numbers

From NSIS Wiki
Jump to navigationJump to search
Author: Afrow UK (talk, contrib)


Note

This script fails on 1.0 and 1.00 and I haven't had time to fix it (probably never will). Instead I very much recommend Instructor's VersionCompare script, which works flawlessly.

Description

Please note that this function has been superseded by the VersionCompare function.

This was made after this topic where RobGrant found that the old VersionCheck function would fail on version numbers like 6 and 6.0 (it would say 6 is lower than 6.0).

Tested Version Numbers

Current VersionCheck function will compare the following version strings successfully...

  • "3.1.0" "3.10.0"
  • "0.6" "6"
  • "6.0" "6"
  • "5.1.1.0" "5.1.1.0"
  • "9.1" "9.1.9"
  • "1.01" "1.10"
  • "2.5.09" "2.5"
  • "2.5.9" "2.5.01"
  • "7" "10.0.0.3646"
  • "1" "11"
  • "91.1.1.1" "101.1.1.1"
  • "1.001" "1.0"

Usage

${VersionCheckNew} "version 1" "version 2" "$[output variable]"

Example

${VersionCheckNew} "1.0.1" "1.1.0" "$R0"
MessageBox MB_OK "Return value: $R0" ;outputs 2

Output values

Output Meaning
0 Versions are equal
1 Version 1 is newer
2 Version 2 is newer

The Function (and Macro)

!macro VersionCheckV5 Ver1 Ver2 OutVar
 Push "${Ver1}"
 Push "${Ver2}"
  Call VersionCheckV5
 Pop "${OutVar}"
!macroend
!define VersionCheckNew "!insertmacro VersionCheckV5"
 
Function VersionCheckV5
 Exch $R0 ; second version number
 Exch
 Exch $R1 ; first version number
 Push $R2
 Push $R3
 Push $R4
 Push $R5 ; second version part
 Push $R6 ; first version part
 
  StrCpy $R1 $R1.
  StrCpy $R0 $R0.
 
 Next: StrCmp $R0$R1 "" 0 +3
  StrCpy $R0 0
  Goto Done
 
  StrCmp $R0 "" 0 +2
   StrCpy $R0 0.
  StrCmp $R1 "" 0 +2
   StrCpy $R1 0.
 
 StrCpy $R2 0
  IntOp $R2 $R2 + 1
  StrCpy $R4 $R1 1 $R2
  StrCmp $R4 . 0 -2
    StrCpy $R6 $R1 $R2
    IntOp $R2 $R2 + 1
    StrCpy $R1 $R1 "" $R2
 
 StrCpy $R2 0
  IntOp $R2 $R2 + 1
  StrCpy $R4 $R0 1 $R2
  StrCmp $R4 . 0 -2
    StrCpy $R5 $R0 $R2
    IntOp $R2 $R2 + 1
    StrCpy $R0 $R0 "" $R2
 
 IntCmp $R5 0 Compare
 IntCmp $R6 0 Compare
 
 StrCpy $R3 0
  StrCpy $R4 $R6 1 $R3
  IntOp $R3 $R3 + 1
  StrCmp $R4 0 -2
 
 StrCpy $R2 0
  StrCpy $R4 $R5 1 $R2
  IntOp $R2 $R2 + 1
  StrCmp $R4 0 -2
 
 IntCmp $R3 $R2 0 +2 +4
 Compare: IntCmp 1$R5 1$R6 Next 0 +3
 
  StrCpy $R0 1
  Goto Done
  StrCpy $R0 2
 
 Done:
 Pop $R6
 Pop $R5
 Pop $R4
 Pop $R3
 Pop $R2
 Pop $R1
 Exch $R0 ; output
FunctionEnd

Version History

  • v5: 28 March 05
    • Re-wrote code again after a lot of thinking :p. Was failing for 1 and 11 (would say 1 was bigger than 11!)
    • Changed function name to VersionCheckV5
    • Fixed small bug (thanks Instructor!)
  • v4: 22 March 05
    • Check per digit would fail on 10.0 and 7.0 (thanks Daijoubu; it would say 1st was smaller).
      The code has now been replaced with a more advanced method.
  • v3: 13 Feb 05
    • Added check per digit per number.
    • Changed function name back to VersionCheckNew.
  • v2: 10 Jan 05
    • Re-written whole function.
    • Changed function name to VersionCheckNew2.
  • v1: 9 Dec 04
    • First version.

Bug Reports

If you find any problems, please send me a Private Message via the NSIS forums.

-Stu (Afrow UK)