我正在寻找一种快速列出文件夹子文件夹中所有文件的方法。基于FileSystemObject的方法太慢 - 从网络驱动器(内联网)列出416文件名大约需要6分钟,因为我知道Dir()函数不允许循环遍历子文件夹。
答案 0 :(得分:5)
改为使用CMD:
Sub SO()
Const parentFolder As String = "C:\Users\bloggsj\folder\" '// 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