本文介绍了VBA 在没有 FileSystemObject 的子文件夹中列出所有文件(快速方式)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在寻找一种快速列出文件夹子文件夹中所有文件的方法.基于 FileSystemObject 的方法太慢了 - 从网络驱动器(内联网)列出 416 个文件名需要大约 6 分钟,据我了解 Dir() 函数不允许循环遍历子文件夹.
I'm searching for a way for fast listing all files from subfolders of folder. Approach based on FileSystemObject is too slow - it takes ~6 minutes for listing 416 filenames from a network drive (intranet), as I understand Dir() function doesn't allow for looping throught subfolders.
推荐答案
改用 CMD:
Sub SO()
Const parentFolder As String = "C:Usersloggsjfolder" '// change as required, keep trailing slash
Dim results As String
results = CreateObject("WScript.Shell").Exec("CMD /C DIR """ & parentFolder & "*.*"" /S /B /A:-D").StdOut.ReadAll
Debug.Print results
'// uncomment to dump results into column A of spreadsheet instead:
'// Range("A1").Resize(UBound(Split(results, vbCrLf)), 1).Value = WorksheetFunction.Transpose(Split(results, vbCrLf))
'//-----------------------------------------------------------------
'// uncomment to filter certain files from results.
'// Const filterType As String = "*.exe"
'// Dim filterResults As String
'//
'// filterResults = Join(Filter(Split(results, vbCrLf), filterType), vbCrLf)
'//
'// Debug.Print filterResults
End Sub
这篇关于VBA 在没有 FileSystemObject 的子文件夹中列出所有文件(快速方式)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!