EXCEL-VBA:递归遍历文件夹及子文件夹中的文件

【引用地址】https://blog.csdn.net/jyh_jack/article/details/78037983
Const SearchPath = "D:\PDF"

Dim DicList, FileList, I, FileName(), FilePath()
Set DicList = CreateObject("Scripting.Dictionary")
Set FileList = CreateObject("Scripting.Dictionary")

DicList.Add SearchPath, ""  '初始化目录

'**************遍历所有目录*******************
I = 0
Do While I < DicList.Count
    Key = DicList.Keys '本次要遍历的目录
    NowDic = Dir(Key(I), vbDirectory) '开始查找
    Do While NowDic <> ""
        If (NowDic <> ".") And (NowDic <> "..") Then
            If (GetAttr(Key(I) & NowDic) And vbDirectory) = vbDirectory Then '找到子目录,则添加
                DicList.Add Key(I) & NowDic & "\", ""
            End If
        End If
        NowDic = Dir() '再找
    Loop
    I = I + 1
Loop
'****************************************************

'**************遍历目录中的所有文件*******************
For Each Key In DicList.Keys '查找所有目录中的文件
   NowFile = Dir(Key & "*.pdf")
   Do While NowFile <> ""
        FileList.Add NowFile, Key 'Add(Key,Item)  FileList.Key=文件名,FileList.Item=目录
        NowFile = Dir()
   Loop
Next
FileName() = FileList.Keys
FilePath() = FileList.Items
'****************************************************

你可能感兴趣的:(EXCEL-VBA:递归遍历文件夹及子文件夹中的文件)