1

Wondering if anyone can advise what code would allow me to: Count the number of items in a specific folder - files as well as folders but not the sub folders. Then I want the result to show that number in a cell minis value by 3.

I have tried a few different things but can't seem to work out how to get all items. Sorry I am very inexperienced.

  • Does this answer your question? [count how many of files in each folder and subfolder then display separately](https://stackoverflow.com/a/38406605/8422953) – braX Apr 21 '23 at 03:22

1 Answers1

2

Count Files and SubFolders

  • Using the FileSystemObject object, you can count in the following way:
Sub CountFilesAndSubfolders()

    Const SRC_PATH As String = "C:\Test\"
    
    Dim fso As Object: Set fso = CreateObject("Scripting.FileSystemObject")
    If Not fso.FolderExists(SRC_PATH) Then Exit Sub
    
    Dim fsoFolder As Object: Set fsoFolder = fso.GetFolder(SRC_PATH)
    
    With fsoFolder
        Debug.Print .SubFolders.Count + .Files.Count
    End With

End Sub
VBasic2008
  • 44,888
  • 5
  • 17
  • 28