通过 VBS 搜索 newer/higher 版本的文件夹
Search for newer/higher version of Folder through VBS
我想编写一个脚本来搜索较新版本的文件夹。但是,我不知道如何启动它。基本上我的目录中有 3 个文件夹。
15.0.4727.1002, 15.0.4701.1002, 15.0.4675
我想搜索更新或更高版本的文件夹。在这种情况下是
15.0.4727.1002
由于您只需要集合(子文件夹)中的一个元素(文件夹),因此排序就显得多余了。但是您需要将文件夹名称转换为(正确)可排序的名称。此子任务在链接到的答案中得到解决。
可以通过格式化(填充)文件夹名称的部分来完成转换:
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
输出:
cscript 31720684.vbs
latest: 15.0.4727.1002
更新评论:
通过将 oLst
设置为 Nothing before 循环,我可以测试它 after 循环以防止 empty/no子文件夹目录。或者:如果我想使用 oLst.Name
我应该确保 oLst
是一个(可用的)对象。
我想编写一个脚本来搜索较新版本的文件夹。但是,我不知道如何启动它。基本上我的目录中有 3 个文件夹。
15.0.4727.1002, 15.0.4701.1002, 15.0.4675
我想搜索更新或更高版本的文件夹。在这种情况下是
15.0.4727.1002
由于您只需要集合(子文件夹)中的一个元素(文件夹),因此排序就显得多余了。但是您需要将文件夹名称转换为(正确)可排序的名称。此子任务在链接到的答案中得到解决。
可以通过格式化(填充)文件夹名称的部分来完成转换:
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
输出:
cscript 31720684.vbs
latest: 15.0.4727.1002
更新评论:
通过将 oLst
设置为 Nothing before 循环,我可以测试它 after 循环以防止 empty/no子文件夹目录。或者:如果我想使用 oLst.Name
我应该确保 oLst
是一个(可用的)对象。