解决从集合(数组、文件夹、..)中获取极值(最大、最近使用的......)问题的起点应该是成语/模式:
Dim sDir : sDir = "..\data\12203427"
Dim oSDir
For Each oSDir In goFS.GetFolder(sDir).SubFolders
WScript.Echo "Saw:", oSDir.DateCreated, oSDir.Name
Next
输出:
Saw: 30.08.2012 21:42:59 devbuild-15.10
Saw: 30.08.2012 21:43:24 DEVBUILD-15.09
Saw: 30.08.2012 21:44:21 devbuild-9.123
Saw: 30.08.2012 21:31:29 DebugBuild-15.0
Saw: 30.08.2012 21:24:00 DevBuild-14.0
所有额外的变量(文件夹、文件夹名称、...)都容易出错。
要使用 .DateCreated 获取“最后一个”子文件夹,只需插入
- 跟踪最新文件夹的 .DateCreated 的日期(初始化为一个小的超出可能范围的日期)
- 一个 If 语句来记录迄今为止最新的文件夹
要根据名称/前缀过滤文件夹,请添加
- 前缀及其长度
- 一个 If 语句,以避免不匹配的文件夹(名称)
在代码中:
Dim sDir : sDir = "..\data\12203427"
Dim dtMR : dtMR = #1/1/1970#
Dim sMRDir : sMRDir = ""
Dim sPfx : sPfx = "devbuild-"
Dim nPfx : nPfx = Len(sPfx)
Dim oSDir
For Each oSDir In goFS.GetFolder(sDir).SubFolders
WScript.Echo "Saw:", oSDir.Name
If 0 = StrComp(sPfx, Left(oSDir.Name, nPfx), vbTextCompare) Then
WScript.Echo Space(4), "DC:", oSDir.DateCreated
If dtMR < oSDir.DateCreated Then
dtMR = oSDir.DateCreated
sMRDir = oSDir.Name
Else
WScript.Echo Space(4), "older then", dtMR
End If
Else
WScript.Echo Space(4), "does not start with", sPfx
End If
Next
WScript.Echo "-----------"
If "" = sMRDir Then
WScript.Echo "no subfolder found"
Else
WScript.Echo "found", sMRDir, dtMR
End If
输出:
Saw: devbuild-15.10
DC: 30.08.2012 21:42:59
Saw: DEVBUILD-15.09
DC: 30.08.2012 21:43:24
Saw: devbuild-9.123
DC: 30.08.2012 21:44:21
Saw: DebugBuild-15.0
does not start with devbuild-
Saw: DevBuild-14.0
DC: 30.08.2012 21:24:00
older then 30.08.2012 21:44:21
-----------
found devbuild-9.123 30.08.2012 21:44:21
要依赖文件夹名称的版本,请遵循相同的策略,但使用适当的数据类型(双版本、RegExp 过滤器和提取器):
Dim sDir : sDir = "..\data\12203427"
Dim dblMR : dblMR = -1.0
Dim sMRDir : sMRDir = ""
Dim reCut : Set reCut = New RegExp
reCut.IgnoreCase = True
reCut.Pattern = "^DevBuild-(\d+\.\d+)$"
Dim oSDir
For Each oSDir In goFS.GetFolder(sDir).SubFolders
WScript.Echo "Saw:", oSDir.Name
Dim oMTS : Set oMTS = reCut.Execute(oSDir.Name)
If 1 = oMTS.Count Then
Dim dblCVers : dblCVers = CDbl(oMTS(0).SubMatches(0))
WScript.Echo Space(4), "Version:", dblCVers
If dblMR < dblCVers Then
dblMR = dblCVers
sMRDir = oSDir.Name
Else
WScript.Echo Space(4), "older then", dblMR
End If
Else
WScript.Echo Space(4), "does not match", reCut.Pattern
End If
Next
WScript.Echo "-----------"
If "" = sMRDir Then
WScript.Echo "no subfolder found"
Else
WScript.Echo "found", sMRDir, dblMR
End If
输出:
Saw: devbuild-15.10
Version: 15,1
Saw: DEVBUILD-15.09
Version: 15,09
older then 15,1
Saw: devbuild-9.123
Version: 9,123
older then 15,1
Saw: DebugBuild-15.0
does not match ^DevBuild-(\d+\.\d+)$
Saw: DevBuild-14.0
Version: 14
older then 15,1
-----------
found devbuild-15.10 15,1
(!德语语言环境!)