Jump to content

Recommended Posts

Posted

In writing an update script, I discovered it wasn't interpreting that a particular file was out of date because the direct comparison of a file whose version was '8.0.0.912' was being considered as a NEWER (greater than) version '8.0.0.1009'

I did a quick search of the forums, but didn't really find anything for comparing (numeric) file versions, so wrote the following.

$Title = 'Version Compare Test'


$FirstVer = InputBox ( $Title, 'Enter first version number:', '8.0.0.912' )
$SecondVer = InputBox ( $Title, 'Enter second version number:', '8.0.0.1009' )

If $FirstVer > $SecondVer Then
    MsgBox ( 0, $Title, 'String compare, ' & $FirstVer & ' is GREATER THAN ' & $SecondVer )
ElseIf $FirstVer < $SecondVer Then
    MsgBox ( 0, $Title, 'String compare, ' & $FirstVer & ' is LESS THAN ' & $SecondVer )
Else
    MsgBox ( 0, $Title, 'String compare, ' & $FirstVer & ' is EQAL TO ' & $SecondVer )
EndIf

$iFirstVer = Number ( $FirstVer )
$iSecondVer = Number ( $SecondVer )

If $iFirstVer > $iSecondVer Then
    MsgBox ( 0, $Title, 'Number compare, ' & $iFirstVer & ' is GREATER THAN ' & $iSecondVer )
ElseIf $iFirstVer < $iSecondVer Then
    MsgBox ( 0, $Title, 'Number compare, ' & $iFirstVer & ' is LESS THAN ' & $iSecondVer )
Else
    MsgBox ( 0, $Title, 'Number compare, ' & $iFirstVer & ' is EQAL TO ' & $iSecondVer )
EndIf

$Ret = _VersionCompare ( $FirstVer, $SecondVer )
If @error Then
    MsgBox ( 0, $Title, 'Version compare error (' & @error & ')' )
ElseIf $Ret = 1 Then
    MsgBox ( 0, $Title, $FirstVer & ' is LESS THAN ' & $SecondVer )
ElseIf $Ret = -1 Then
    MsgBox ( 0, $Title, $FirstVer & ' is GREATER THAN ' & $SecondVer )
Else
    MsgBox ( 0, $Title, $FirstVer & ' is EQUAL TO ' & $SecondVer )
EndIf

;===============================================================================
;
; Function Name:    _VersionCompare()
; Description:    Compare two file version number strings (like the ones returned by FileGetVersion().
; Parameter(s):  $sVer1 - First version string
;                  $sVer2 - Second version string
;                  $cSepChr - Optional separator character (default is .)
; Requirement(s):   <math.au3>
; Return Value(s):  Return -1 if the first version string is larger than the second,
;                  Returns 1 if the second version string is larger than the first,
;                   Returns 0 if the version strings are equal
; ERROR RETURNS:    In the unlikely situation there is an error,
;                   @error is set and 0 is returnd.
;                   Situations that would cause an error:
;                   $cSepChr is longer than one character
; Author(s):        JerryD
;
;===============================================================================
;
#include <math.au3>
Func _VersionCompare ( $sVer1, $sVer2, $cSepChr = '.' )
; In case old style defaults are used...
    If $cSepChr = '' OR $cSepChr = -1 Then
        $cSepChr = '.'
    EndIf
    Local $iMaxCount, $iMaxVer
    Local $aVer1, $aVer2
    Local $iRetError_InvalidSeparator = 1
    Local $iRetError_InvalidSepCharacter = 2
    Local $iRetError_Ver1NotNumber = 3
    Local $iRetError_Ver2NotNumber = 4
    
; Check the seperator character for problems
    If StringLen ( $cSepChr ) > 1 Then
        SetError ( $iRetError_InvalidSeparator )
        Return 0
    EndIf
    If IsNumber ( $cSepChr ) Then
        SetError ( $iRetError_InvalidSepCharacter )
        Return 0
    EndIf
    
; Break the versions into arrays
    $aVer1 = StringSplit ( $sVer1, $cSepChr )
    $aVer2 = StringSplit ( $sVer2, $cSepChr )
    
; Make sure all the version elements are numbers
; (have to convert them to an integer anyway
    For $i = 1 to $aVer1[0]
        If NOT StringIsDigit ( $aVer1[$i] ) Then
            SetError ( $iRetError_Ver1NotNumber )
            Return 0
        Else
            $aVer1[$i] = Int ( $aVer1[$i] )
        EndIf
    Next
    For $i = 1 to $aVer2[0]
        If NOT StringIsDigit  ( $aVer2[$i] ) Then
            SetError ( $iRetError_Ver2NotNumber )
            Return 0
        Else
            $aVer2[$i] = Int ( $aVer2[$i] )
        EndIf
    Next
    
; Now compare the numbers in the array
; Return -1 if first version number is greater than second version number
; Return 1 if second version number is greater than first version number
    $iMaxCount = _Min ( $aVer1[0], $aVer2[0] )
    For $i = 1 to $iMaxCount
        If $aVer1[$i] > $aVer2[$i] Then
            Return -1
        ElseIf $aVer1[$i] < $aVer2[$i] Then
            Return 1
        EndIf
    Next
    Return 0
EndFunc

If there's something (more standard) already built, I'm all ears! Otherwise, your (constructive) feedback is welcome.

Jerry

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...