问题描述
我正在寻找一个 VBA 脚本,该脚本将遍历指定文件夹的所有子文件夹.当我说所有子文件夹时,我的意思是指定文件夹内的每个文件夹,其中的每个文件夹,以及其中的每个文件夹......理论上可能有无限嵌套的子文件夹,但实际上它可能不会超过 3或 4. 我正在使用 VBA Scripting Runtime 对象,因此一旦我循环进入该文件夹,我就可以检查某些文件的属性(但我知道如何执行该部分).
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).
感谢您的帮助!
这个问题不同于前面问题中列出的类似"问题包含已知目录,而这里需要的是找到已知和未知目录.还需要多层子目录.你们真的应该在启动重复"之前阅读问题.
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".
推荐答案
只是一个简单的文件夹下钻.
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
这篇关于使用 VBA 循环遍历所有子文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!