VIP

[SOLVED] Find largest version? _VersionCompare()

10 posts in this topic

#1 ·  Posted (edited)

How to Find the largest version of a list of version numbers?

 
;For TEST
DirCreate("1.2.6.4")
DirCreate("1.1.2.4")
DirCreate("1.0.3.4")
DirCreate("1.5.0.4")
DirCreate("1.6.7.4")
;For TEST END


#include <Misc.au3>
#include <File.au3>
Local $sListVer,$sListFile = _FileListToArray(@ScriptDir, "*", 2, 0)
For $x = 1 To UBound($sListFile) - 1
    If StringInStr($sListFile[$x],".") Then
    ConsoleWrite($sListFile[$x]&@CRLF)
    $sListVer&=$sListFile[$x]&"|"
    EndIf
Next

Local $sVerList = StringSplit($sListVer,"|")
If UBound($sVerList) > 1 Then
    For $x = 1 To UBound($sVerList) - 1
        ConsoleWrite($sVerList[$x]&@CRLF)
        ;ConsoleWrite(_VersionCompare ( "", "" )&@CRLF)
    Next
EndIf


;For TEST
DirRemove("1.2.6.4")
DirRemove("1.1.2.4")
DirRemove("1.0.3.4")
DirRemove("1.5.0.4")
DirRemove("1.6.7.4")
;For TEST END

#

Edited by Trong

Regards,
 

Share this post


Link to post
Share on other sites



Use _VersionCompare and write the largest version into a variable. After the end of the loop the variable holds the largest version.

1 person likes this

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

For a generalized way of sorting with a user defined comparison function, see #7 in this thread

for the attachment ArraySortCustom.au3

Edit: In fact the example compare function is for dot separated version strings

 

 

 

Edited by MilesAhead
1 person likes this

Share this post


Link to post
Share on other sites
#include <Array.au3>

$sVersions = "1.2.6.4|1.1.2.4|1.0.3.4|1.5.0.4|1.6.7.4"

$aVersions1 = StringSplit($sVersions, "|", 2)

_ArraySort($aVersions1, 1)

MsgBox(0, "Latest version", $aVersions1[0])

 

1 person likes this

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

#include <Array.au3>

$sVersions = "1.2.6.4|1.1.2.4|1.0.3.4|1.5.0.4|1.6.7.4"

$aVersions1 = StringSplit($sVersions, "|", 2)

_ArraySort($aVersions1, 1)

MsgBox(0, "Latest version", $aVersions1[0])

 

This works in your example but not if the sample version strings have a higher number in a version compnent that starts with a character lower in the sort order.  As example 3.3.2.4 would be seen as higher than 3.12.2.4

 

That is why in the example comparison function I split out the version components and converted them to numbers.

 

 

Edited by MilesAhead
1 person likes this

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Agreed.

I just coded it down a bit to fit.

Stringreplace the "." and sort it as an int.

Sorry.  I looked at your code without really seeing it.  Must've been a pre-coffee scan.  :)

 

Edited by MilesAhead
Double text fix.
1 person likes this

Share this post


Link to post
Share on other sites

Agreed.

I just coded it down a bit to fit.

Stringreplace the "." and sort it as an int.

That would still break if the lower number is longer

$versionett = "3.3.8.1"
$versiontva = "3.2.12.0"

$versionett = Int(StringReplace($versionett, ".", ""))
$versiontva = Int(StringReplace($versiontva, ".", ""))

ConsoleWrite("$versionett is higher than $versiontva? " & ($versionett > $versiontva) & @CRLF)

You must split the parts like _VersionCompare is doing if the comparison is going to work

1 person likes this

Share this post


Link to post
Share on other sites

have a look here for one of possible ways: https://www.autoitscript.com/forum/topic/166640-how-to-format-the-output-of-the-autoitversion-macro/

p.s.

how could be modified Yashied's regex, (from post #7 of above topic) so to have numbers padded with zero for a total of 3 digits in length instead of only 2 ??

1 person likes this

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Share this post


Link to post
Share on other sites

Not sure if its what you want :unsure::

#include <File.au3>
#include <MsgBoxConstants.au3>

Local Const $FOLDER = @ScriptDir ; Folder to check

DirCreate("1.2.6.4")
DirCreate("1.1.2.4")
DirCreate("1.0.3.4")
DirCreate("1.5.0.4")
DirCreate("1.6.7.4")

Local $aVersions = _FileListToArray($FOLDER, "*.*.*.*", $FLTA_FOLDERS)
Local $iLatestVersionsIndex = 0
For $i = 1 To $aVersions[0]
    If Number(StringReplace($aVersions[$iLatestVersionsIndex], '.', "")) < Number(StringReplace($aVersions[$i], '.', "")) Then
        $iLatestVersionsIndex = $i
    EndIf
Next

MsgBox($MB_OK, "Latest Version", $aVersions[$iLatestVersionsIndex])

DirRemove("1.2.6.4")
DirRemove("1.1.2.4")
DirRemove("1.0.3.4")
DirRemove("1.5.0.4")
DirRemove("1.6.7.4")

TD :)

1 person likes this

AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Spoiler

My contributions to the AutoIt Community

Some messages & Apologizes:

If I hurt you, Please accept my apologies, I never (regardless of the situation) mean to hurt anybody!!!

Also, I am very busy with my project so I will appear in the last row of the online list, if you want to contact me: Email@TheDcoder.xyz

Or you can have a nice chat with me in freenode, I use the same nick on freenode too!

3fHNZJ.gif

PLEASE JOIN ##AutoIt AND HELP THE IRC AUTOIT COMMUNITY!

Share this post


Link to post
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