显示文件夹属性:“包含”

我正在使用以下代码来计算文件夹中的文件数量。 但是,如果有超过500个文件,则很难循环遍历每个文件。 所以我的问题是,是否有可能获取文件夹属性 – “包含”,而不是循环通过每个文件。

Sub pdfcount() Dim FolderPath As String, path As String, count As Integer FolderPath = "C:\Documents and Settings\FPY\" path = FolderPath &"\*.pdf" Filename = Dir(path) Do While Filename <>"" count = count +1 Filename = Dir() Loop Range("A1").Value = count End Sub 

下面的函数将返回一个文件夹中的文件数量。

 Function CountFiles(folderPath As String) As Long Dim fso As Object Dim files As Object Set fso = CreateObject("Scripting.FileSystemObject") Set files = fso.GetFolder(folderPath).Files CountFiles = files.Count End Function