本帖最后由 yu2n 于 2017-5-5 14:28 编辑
回复 2# 523066680
重複使用同一個 fso,還能更快一些:- Option Explicit
-
- Call CommandMode()
-
- Test
- Sub Test()
- Dim fd1, dt1, dt2, arr
- fd1 = "D:\"
- dt1 = Timer()
- arr = ScanFolder(fd1)
- dt2 = Timer()
- WScript.Echo "文件、文件夾個數:" & UBound(arr) & vbCrLf & _
- "耗時:" & (dt2 - dt1) & " 秒"
- End Sub
-
- '************************************************************************
- 'FSO 获取指定文件夹下,所有文件、文件夹的路径(返回一维数组列表)
- '************************************************************************
- Function ScanFolder(ByVal strFolder)
- Dim fso, arrList()
- ReDim Preserve arrList(0)
- Set fso = CreateObject("Scripting.FileSystemObject")
- If fso.FolderExists(strFolder) Then
- arrList(0) = fso.GetFolder(strFolder).Path & "\"
- Call DO_SCAN_FOLDER(fso, arrList, strFolder)
- End If
- ScanFolder = arrList
- End Function
- Sub DO_SCAN_FOLDER(ByRef fso, ByRef arr, ByVal str)
- Dim oItems, oFile, oFolder
- On Error Resume Next
- Set oItems = fso.GetFolder(str)
- For Each oFile In oItems.Files
- ReDim Preserve arr(UBound(arr) + 1)
- arr(UBound(arr)) = oFile.Path
- Next
- For Each oFolder In oItems.subfolders
- ReDim Preserve arr(UBound(arr) + 1)
- arr(UBound(arr)) = oFolder.Path & "\"
- Call DO_SCAN_FOLDER(fso, arr, oFolder.Path & "\")
- Next
- End Sub
-
-
- '************************************************************************
- '命令行模式运行
- '************************************************************************
- Sub CommandMode()
- If InStr(1, WScript.FullName, "\cscript.exe", vbTextCompare) > 0 Then Exit Sub
- CreateObject("WScript.Shell").Run "cmd /c title " & WScript.ScriptName & _
- " & cscript //nologo """ & WScript.ScriptFullName & """ & pause", 1, False
- WScript.Quit(0)
- End Sub
复制代码 測試結果如下:- 文件、文件夾個數:188575
- 耗時:60.46875 秒
复制代码 感覺還是慢了,這似乎是FSO的極限了?我以為 WMI 能更快的,結果一試~尷尬了。 |