VBA List all files (fast way) in subfolders without FileSystemObject

蓝咒 提交于 2019-12-17 20:37:09

问题


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.


回答1:


Use CMD instead:

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


来源:https://stackoverflow.com/questions/31132775/vba-list-all-files-fast-way-in-subfolders-without-filesystemobject

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!