1

我是 VBScript 的新手,我正在尝试编写代码,该代码将返回给定文件夹中与给定命名模式匹配的最新子文件夹的路径。

基本上我希望它打开给定的文件夹并返回名称以“DevBuild”开头的最新子文件夹的路径。例如,如果给定文件夹有 3 个文件夹:

DevBuild-14.0  
DevBuild-15.0  
DebugBuild-15.0

我希望它返回“{Path to given folder}\DevBuild-15.0\”,因为它是以“DevBuild”开头的最新文件夹。“最新”是指具有最近创建日期的文件夹,或者如果它更容易,则使用版本号。哪个没关系。

到目前为止,我有这个:

Dim fso, folder, subfolders
Set fso = CreateObject("Scripting.FileSystemObject")
Set folder = fso.GetFolder("B:\")
Dim count
count = folder.SubFolders.Count - 1
Dim folderNames()
ReDim folderNames(count)
Set subfolders = folder.SubFolders

'MsgBox UBound(folderNames)

For i = 0 to count
    MsgBox subfolders(i).Name
Next

不幸的是,我遇到了一个错误MsgBox subfolders(i).Name,我认为这是因为我不明白如何从subfolders数组中获取一个元素。

4

2 回答 2

1

解决从集合(数组、文件夹、..)中获取极值(最大、最近使用的......)问题的起点应该是成语/模式:

  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 获取“最后一个”子文件夹,只需插入

  1. 跟踪最新文件夹的 .DateCreated 的日期(初始化为一个小的超出可能范围的日期)
  2. 一个 If 语句来记录迄今为止最新的文件夹

要根据名称/前缀过滤文件夹,请添加

  1. 前缀及其长度
  2. 一个 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

(!德语语言环境!)

于 2012-08-30T20:20:30.003 回答
0

你的一些代码有点多余。这就是我将如何做到这一点。注意 For Each 块。

Dim fso, folder, subfolders, subfolder
Set fso = CreateObject("Scripting.FileSystemObject")
Set folder = fso.GetFolder("B:\")

Set subfolders = folder.SubFolders

Dim folderNames(subfolders.Count)

dim i
i = 0
For Each subfolder in subfolders
    folderNames(i) = subfolder.Name
    i = i + 1
Next
于 2012-08-30T19:26:45.920 回答