Jump to content

CompareVersions UDF


therks
 Share

Recommended Posts

I just tossed this script together, I needed a way to compare two versions of something, to decide which was newer (It will be used for the app. I'm making for this post). I figured I'd post it since I thought it might be handy for someone else, or I could see some ideas on how other people have done it. Perhaps I went about it all wrong, I'm not sure.

Anyway, here's the code:

Update below

So here we go.

The 3rd argument is a string defining what delimits the chunks of the version number. Default is a period.

The 4th argument is a 0/1 flag.

If it's 0 (default) then the function will return the higher/newer version entered.

If it's 1 then it will do the opposite.

The 5th argument is a string for what will be returned if the two versions are equal. It currently defaults to the equal character (=).

*Edit: Cleaned it up a bit, and added a few things.

Edited by Saunders
Link to comment
Share on other sites

Updated:

Another update

Returns:

Success: Returns the greater version string (or if i_Flag is 1, the lesser version string), or an empty string if the two versions were equal.

Failure: If it encounters a non-number segment in the version string, it will throw an error. The error is 1 if the first version string caused the error, and 2 if the second caused the error.

*Edit: Updated again. I realized just now that the IsNumber isn't used to recognize if a string contains numeric values, it actually checks to see if the variable type is a number as well. Stupid me. Works now though.

Edited by Saunders
Link to comment
Share on other sites

  • 2 weeks later...

Okay, so here we go again. I realized that the old function had a bit of a problem with some version numbers when the latest AutoIt came out (3.1.0.1) it was telling me that an older version (3.0.103.173) was newer because of the math I was using. It turns out I'm not the math whiz I thought I was (which is pretty bad cus I knew I wasn't a math whiz in the first place), so here's a more simple, makes-sense version of the function. And so far, no bugs.

*Edit: Updated again. The method I was using before would see 3.0.2 as lower than 3.0.101. Now it should work fine.

Func _CompareVersions($s_Vers1, $s_Vers2)
    Local $i, $i_Vers1, $i_Vers2, $i_Top
    Local $a_Vers1 = StringSplit($s_Vers1, '.')
    Local $a_Vers2 = StringSplit($s_Vers2, '.')

    $i_Top = $a_Vers1[0]
    If $a_Vers1[0] < $a_Vers2[0] Then
        $i_Top = $a_Vers2[0]
    EndIf
    
    For $i = 1 To $i_Top
        If $i = 1 Then
            $i_Vers1 = Int($a_Vers1[$i])
            $i_Vers2 = Int($a_Vers2[$i])
        Else
            $i_Vers1 = 0
            $i_Vers2 = 0
            If $i <= $a_Vers1[0] Then
                $i_Vers1 = Number('.' & $a_Vers1[$i])
            EndIf
            If $i <= $a_Vers2[0] Then
                $i_Vers2 = Number('.' & $a_Vers2[$i])
            EndIf
        EndIf
        
        If $i_Vers1 > $i_Vers2 Then
            $s_Return = $s_Vers1
            ExitLoop
        ElseIf $i_Vers1 < $i_Vers2 Then
            $s_Return = $s_Vers2
            ExitLoop
        Else
            $s_Return = ''
        EndIf
    Next

    Return $s_Return
EndFunc

Example code:

#include <CompareVersions.au3>

$notepad = FileGetVersion('C:\windows\system32\notepad.exe')
$regedit = FileGetVersion('C:\windows\system32\regedit.exe')

$version = _CompareVersions($notepad, $regedit)

If $version = $notepad Then
    MsgBox(0, '', 'Notepad has a higher version number than regedit')
ElseIf $version = $regedit Then
    MsgBox(0, '', 'Regedit has a higher version number than notepad')
Else
    MsgBox(0, '', 'Notepad and regedit have equal version numbers')
EndIf
Edited by Saunders
Link to comment
Share on other sites

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
 Share

  • Recently Browsing   0 members

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