使用 VBA 循环遍历所有子文件夹
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22645347/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Loop Through All Subfolders Using VBA
提问by Jake
I'm looking for a VBA script that will loop through all subfolders of a specified folder. When I say all subfolders, I mean each folder inside the specified folder, and each folder inside of that, and each folder inside of that...in theory there could be infinite nested subfolders, but in actuality it will probably not go above 3 or 4. I'm using the VBA Scripting Runtime objects, so that once I loop into the folder I can check properties of some files (but I know how to do that part).
我正在寻找一个 VBA 脚本,它将循环遍历指定文件夹的所有子文件夹。当我说所有子文件夹时,我的意思是指定文件夹中的每个文件夹,里面的每个文件夹,里面的每个文件夹......理论上可能有无限嵌套的子文件夹,但实际上它可能不会超过 3或 4. 我正在使用 VBA Scripting Runtime 对象,因此一旦我循环进入文件夹,我就可以检查某些文件的属性(但我知道如何执行该部分)。
Thank you for your help!
感谢您的帮助!
This question is different from the listed "similar" questions in the previous questions contained known directories, whereas the need here was to find known and unknown directories. Also needed multiple layers of subdirectories. You guys really should just read the question before you fire off "duplicate".
这个问题不同于前面问题中列出的包含已知目录的“类似”问题,而这里需要的是找到已知和未知目录。还需要多层子目录。你们真的应该在启动“重复”之前阅读问题。
回答by Rich
Just a simple folder drill down.
只是一个简单的文件夹向下钻取。
sub sample()
Dim FileSystem As Object
Dim HostFolder As String
HostFolder = "C:\"
Set FileSystem = CreateObject("Scripting.FileSystemObject")
DoFolder FileSystem.GetFolder(HostFolder)
end sub
Sub DoFolder(Folder)
Dim SubFolder
For Each SubFolder In Folder.SubFolders
DoFolder SubFolder
Next
Dim File
For Each File In Folder.Files
' Operate on each file
Next
End Sub
回答by Cor_Blimey
And to complement Rich's recursive answer, a non-recursive method.
并补充 Rich 的递归答案,一种非递归方法。
Public Sub NonRecursiveMethod()
Dim fso, oFolder, oSubfolder, oFile, queue As Collection
Set fso = CreateObject("Scripting.FileSystemObject")
Set queue = New Collection
queue.Add fso.GetFolder("your folder path variable") 'obviously replace
Do While queue.Count > 0
Set oFolder = queue(1)
queue.Remove 1 'dequeue
'...insert any folder processing code here...
For Each oSubfolder In oFolder.SubFolders
queue.Add oSubfolder 'enqueue
Next oSubfolder
For Each oFile In oFolder.Files
'...insert any file processing code here...
Next oFile
Loop
End Sub
You can use a queue for FIFO behaviour (shown above), or you can use a stack for LIFO behaviour which would process in the same order as a recursive approach (replace Set oFolder = queue(1)
with Set oFolder = queue(queue.Count)
and replace queue.Remove(1)
with queue.Remove(queue.Count)
, and probably rename the variable...)
您可以将队列用于 FIFO 行为(如上所示),或者您可以将堆栈用于 LIFO 行为,该堆栈将按照与递归方法相同的顺序进行处理(替换Set oFolder = queue(1)
为Set oFolder = queue(queue.Count)
并替换queue.Remove(1)
为queue.Remove(queue.Count)
,并可能重命名变量...)