निम्न कोड FileSystemObject का उपयोग करने से लगभग 19 गुना तेज चलता है। मेरी मशीन पर, तीन अलग-अलग डैक्टरीज़ में 4000 फाइलों को ढूंढने से फाइलसिस्टम ऑब्जेक्ट का उपयोग करके 1.57 सेकेंड लगे, लेकिन इस कोड का उपयोग करके केवल 0.08 सेकेंड।
Public Function CountFilesWithGivenExtension(_
i_strFolderWithTerminalBackslant As String, _
i_strExtensionIncludingPeriod As String _
) As Long
If Len(Dir$(i_strFolderWithTerminalBackslant & "*" _
& i_strExtensionIncludingPeriod)) > 0 Then
CountFilesWithGivenExtension = 1
While Len(Dir$) > 0
CountFilesWithGivenExtension = _
CountFilesWithGivenExtension + 1
DoEvents
Wend
Else
CountFilesWithGivenExtension = 0
End If
End Function
नमूना उपयोग:
Debug.Print CountFilesWithGivenExtension("C:\", ".ex*")
("DoEvents" आवश्यक नहीं है, लेकिन आप रोकें उपयोग करने के लिए/तोड़ अगर जरूरत अनुमति देता है।)
स्रोत
2014-03-10 20:42:40
उन उत्सुक लोगों के लिए जो 'डीबग.प्रिंट' करता है, इसे देखें: http://stackoverflow.com/questions/2916287/where-does-vba-debug-print-log-to – ecoe