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            
推荐阅读
传统的方式配置xlam比较麻烦,这段代码可以让你双击打开xlam时自动安装或更新
vba要实现md5必须引用外部库,导致vba程序交付安装不是很方便。很多时候我们并不是想要取真正的md5,只是取一个加密字符串,比如保存密码的时候。本文中的代码直接利用windows系统内置命令certutil -hashfile实现了一种变相的md5函数。代码要读写本地文件两次,因此效率不高,不过在登陆验证一下密码等场景并没有影响。
评论