VBA查找指定目录下所有文件(迭代)

原创文章,欢迎转载:http://miao.blog/article/vba-find-file

' File System Object             
Private fso As FileSystemObject            
            
Private Sub demo()            
    Set fso = CreateObject("Scripting.FileSystemObject")            
                        
    Call searchFile("D:\3.0")            
End Sub            
            
            
Private Sub searchFiles(folder_path As String)            
    Dim xFolder As folder, subFolder As folder            
    Set xFolder = fso.GetFolder(folder_path)            
                
    ' 查找文件            
    Dim xFile As file            
    For Each xFile In xFolder.Files            
                    
        ' TODO do something            
        Debug.Print xFile.path            
    Next            
            
    ' 查找子目录            
    For Each subFolder In xFolder.SubFolders            
        Call searchFiles(subFolder.path)            
    Next            
End Sub            
推荐阅读
VBA(64位)生成UTF-8文件的代码
VBA发送GET、POST请求的方法
评论