【VBA】Get file count in folder

You can get  “file count in folder“!

PR

VBA Code

As an example, get file count in “test” folder.

"test" folder
“test” folder
There are three files in the folder.
There are 3 files in the folder.
Option Explicit

Sub sample()
    
    Dim targetFolder As String
    Dim fso As Object
    Dim fileCount As Long

    targetFolder = "C:\Users\user\Desktop\test"
    
    Set fso = CreateObject("Scripting.FileSystemObject")
    
    'Get file count in folder
    fileCount = fso.GetFolder(targetFolder).Files.Count
    
    MsgBox ("file count : " & fileCount)
    
    Set fso = Nothing
    
End Sub

Get file count in folder(Line 14).

PR

Result

You got “file count in folder“!

Result
PR

FYI No.1

Also, you can get “folder count in folder“!

For more information, see the following article:

PR

FYI No.2

For more information about following, see the following official online manual:

GetFolder method” of “FileSystemObject”


Files property” of “Folder Object”


●Files collection


Count property” of “collection”

タイトルとURLをコピーしました