Jump to content

[SOLVED] Find largest version? _VersionCompare()


Trong
 Share

Recommended Posts

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,
 

Link to comment
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.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
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])

 

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

Monkey's are, like, natures humans.

Link to comment
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])

 

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

Link to comment
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 ??

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

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

Link to comment
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 :)

EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time)

DcodingTheWeb Forum - Follow for updates and Join for discussion

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...