【VBA】遍历文件夹及子文件夹

1. 需求
遍历文件夹及子文件夹

2. 实现代码

Function recursiveFolder(strFolder)

    Dim fso, objFolder, objFiles, objSubFolder
    Set fso = CreateObject("Scripting.FileSystemObject")

    If fso.FolderExists(strFolder) Then
        Set objFolder = fso.getFolder(strFolder)
        Set objFiles = objFolder.Files
        Set objSubFolder = objFolder.subFolders
        For Each oFile in objFiles
            Debug.print "文件名" & oFile.Name
        Next
        For Each oSubFolder In objSubFolder
            recursiveFolder oSubFolder.Path '递归
        Next 
    End If
End Function

你可能感兴趣的:(VBA学习笔记)