0

I want to write a script that will search for a newer version of folder. However, I have no idea how to start it. Basically I have 3 folders in my directory.

15.0.4727.1002, 15.0.4701.1002, 15.0.4675

I would like to search for the folder that has a newer or higher version. In this is case it's

15.0.4727.1002

xka
  • 53
  • 4
  • 11
  • see http://stackoverflow.com/questions/10659516/how-to-sort-array-of-version-number-strings-in-vbscript and http://stackoverflow.com/questions/20130498/sorting-dictionary-of-strings-and-numbers. – Ekkehard.Horner Jul 30 '15 at 10:16
  • is this also can be applied to folder? *additional information after I've found the latest version folder name i have to access and do something inside the folder. – xka Jul 30 '15 at 10:56

1 Answers1

1

As you just need one element (folder) from the collection (SubFolders), sorting is overkill. But you need to convert the folder name into something (correctly) sortable. This subtask is addressed in the answers is linked to.

The conversion can be done by formatting (padding) the parts of the folder names:

Option Explicit

Dim oFS  : Set oFS  = CreateObject("Scripting.FileSystemObject")
Dim oSB  : Set oSB  = CreateObject("System.Text.StringBuilder")
Dim sLst : sLst     = "" ' smallest possible value
Dim oLst : Set oLst = Nothing

Dim oDir
For Each oDir In oFS.GetFolder("..\f").SubFolders
    Dim aParts : aParts = Split(oDir.Name, ".")
    ReDim Preserve aParts(3)
    oSB.AppendFormat_4 "{0,6}{1,6}{2,6}{3,6}", (aParts)
    Dim sKey   : sKey   = oSB.ToString() : oSB.Length = 0
    If sLst < sKey Then
       sLst     = sKey
       Set oLst = oDir
    End If
Next
If Not oLst Is Nothing Then
   WScript.Echo "latest:", oLst.Name
End If

output:

cscript 31720684.vbs
latest: 15.0.4727.1002

Update wrt comment:

By setting oLst to Nothing before the loop, I can test it after the loop to guard against an empty/no subfolders directory. Or: If I want to use oLst.Name I should make sure that oLst is an (usable) object.

Ekkehard.Horner
  • 38,498
  • 2
  • 45
  • 96
  • This is exactly what im looking for. Im so much thankful for this, you've already helped me twice. Hope i can get better with scripting just like you. one last question I've been wondering for this line of code "If Not oLst Is Nothing Then" – xka Jul 30 '15 at 12:40