3

我正在使用文件系统对象循环遍历一组目录,并且我想指定一个额外的目录来循环遍历。例如,我目前有:

Sub test()
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFolder = objFSO.GetFolder(Directory)
    Set colSubfolders = objFolder.Subfolders
    For Each objSubfolder In colSubfolders
        ' take some action
    End For
End Sub

但是,我想指定一个额外的文件夹来循环,例如:

colSubfolders = colSubfolders + "additionalpath"
For Each objSubfolder In colSubfolders ....

或者,是否可以在循环命令中指定多个对象,例如:

For Each objSubfolder in colSubfolders, "additionalpath"
4

1 回答 1

3

最简单的方法是分解枚举目录的代码和“采取一些行动”的代码,并将其包装在重复您需要的调用中;

Sub foo()
    enumDirs "c:\temp\", "c:\music", "c:\as many as you like ..."
End Sub

Sub enumDirs(ParamArray strPaths() As Variant)
    Dim i As Long
    For i = 0 To UBound(strPaths)
        enumDir CStr(strPaths(i))
    Next
End Sub

Sub enumDir(strPath As String)
    Dim objFolder As Object, colSubfolders As Object, objSubfolder As Object
    Set objFolder = CreateObject("Scripting.FileSystemObject").GetFolder(strPath)
    Set colSubfolders = objFolder.Subfolders
    For Each objSubfolder In colSubfolders
        TakeSomeAction strPath, objSubfolder.Name
    Next
End Sub

sub TakeSomeAction(strRoot As String, strFoundPath As String)
    Debug.Print ">"; strRoot & ", " & strFoundPath
End sub
于 2012-04-24T17:52:48.750 回答