使用VBA遍历所有子文件夹 [英] Loop Through All Subfolders Using VBA

查看:118
本文介绍了使用VBA遍历所有子文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个VBA脚本,该脚本将循环遍历指定文件夹的所有子文件夹.当我说所有子文件夹时,我的意思是指定文件夹内的每个文件夹,该文件夹内的每个文件夹以及该文件夹内的每个文件夹...从理论上讲,可以有无限嵌套的子文件夹,但实际上,它可能不会超过3或4.我使用的是VBA脚本运行时对象,因此一旦循环进入该文件夹,便可以检查某些文件的属性(但是我知道该怎么做).

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遍历所有子文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆