5

I have to compare the versions of current file and already the installed in NSIS installer. I understand the version numbers are in string but I read at couple of places that the NSIS automaticaly converts the strings to intergers if needed in logic operations.

Just for the test purpose, I wrote the following script:

Var Test1
Var Test2

section
    StrCpy $Test1 "4.3.1.50245"
    StrCpy $Test2 "4.2.1.50245"

    ${If} $Test1 > $Test2
        MessageBox MB_ICONSTOP  "$Test1 is bigger than $Test2" 
    ${Else}
        MessageBox MB_ICONSTOP  "$Test2 is bigger than $Test1" 
    ${EndIf}
    Quit
    sectionEnd

PROBLEM: It gives me the result 4.2.1.50245 is greated than 4.3.1.50245. However, it gives me correct results if I try to compare 4.2.1.50245with 3.2.1.50245 (or if I compare 50245and 40256 etc.)

skm
  • 5,015
  • 8
  • 43
  • 104
  • Skm, did the following answer your question, or do you need further assistance? – Damian Jan 12 '17 at 15:23
  • 1
    sorry, I didn't get a chance to check it. I will update once I check it (probably on monday). Thanks :) – skm Jan 12 '17 at 16:36
  • You say it gives you the result that 4.2.1.50245 is greater than 4.3.1.50245, but really it's saying "greater than or equal to" since you don't have an equal option. It thinks they're equal because when it converts the strings to integers they both become 4. – Kyle Delaney Apr 20 '18 at 16:00

1 Answers1

4

Check out the following function, Version Compare http://nsis.sourceforge.net/VersionCompare

Section
  ${VersionCompare} "1.1.1.9" "1.1.1.01" $R0
  ; $R0="1"
SectionEnd
; Result:
;   $var=0  Versions are equal
;   $var=1  Version1 is newer
;   $var=2  Version2 is newer

If not, another method would be to implement an Explode of the string and then compare the Major, Minor, Revision, ... portions. But that may be overkill: http://nsis.sourceforge.net/Explode

Damian
  • 1,209
  • 14
  • 24