【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