Function GetTotalFileCount(folderPath As String) As Long
Dim fso As FileSystemObject
Dim folder As Folder
Dim subFolder As Folder
Dim fileCount As Long
Set fso = New FileSystemObject
Set folder = fso.GetFolder(folderPath)
' 当該フォルダ内のファイル数をカウント
fileCount = folder.Files.Count
' サブフォルダ内のファイルも再帰的にカウント
For Each subFolder In folder.SubFolders
fileCount = fileCount + GetTotalFileCount(subFolder.Path)
Next subFolder
' カウントしたファイル数を返す
GetTotalFileCount = fileCount
' オブジェクトの解放
Set folder = Nothing
Set fso = Nothing
End Function
コメント