显示文件夹属性:"包含"

时间:2016-07-13 11:14:15

标签: excel vba excel-vba

我使用以下代码来计算文件夹中的文件数。但是,如果有超过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

1 个答案:

答案 0 :(得分:1)

以下函数将返回文件夹中的文件数。

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