Announcement

Collapse
No announcement yet.

Count the number of subfolders within a folder

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Count the number of subfolders within a folder



    I'd like to count the number of subfolders within a folder and input that information at cell "B10". For example, W:\My Report contains the subfolders Data, Files, and Records, so it should count 3. Anyone know how to do it?

  • #2
    Re: Count the number of subfolders within a folder

    Hi John,

    How about:
    Code:
    Sub FolderColl()
        Dim oFSO As Object
        Dim folder As Object
        Dim subfolders As Object
        
        Set oFSO = CreateObject("Scripting.FileSystemObject")
        Set folder = oFSO.GetFolder("W:\My Report ")
        Set subfolders = folder.subfolders
        MsgBox subfolders.Count
        
        Set oFSO = Nothing
        Set folder = Nothing
        Set subfolders = Nothing
        'release memory
    
    End Sub
    HTH
    Cross-poster? Read this: Cross-posters
    Struggling to use tags (including Code tags)? : Forum tags

    Comment


    • #3
      Re: Count the number of subfolders within a folder

      Thanks, Richie!

      Comment


      • #4
        Re: Count the number of subfolders within a folder

        I have a similar problem . . .

        Using the terminology of binary trees, is there a way to count only the bottom "leaves" of the subfolders? (i.e. only count the subfolders that do not have any subfolders within themselves)?

        Thanks,
        Bryan
        Last edited by bdonkersgoed; November 23rd, 2005, 04:37.

        Comment


        • #5
          Re: Count the number of subfolders within a folder

          Or, better yet, does anyone know how to count the number of files stored within a given root directory?

          For example, if you specify "c:\" then it should count the number of files stored in drive "c".

          Thanks!
          Bryan

          Comment


          • #6
            Re: Count the number of subfolders within a folder


            Option Explicit

            Private FSO As Object
            Private cFiles(1 To 3) As Long
            Private cFolders(1 To 3) As Long

            Sub Folders()
            Dim i As Long
            Dim sFolder As String
            Dim iStart As Long
            Dim iEnd As Long
            Dim fOutline As Boolean

            cFiles(1) = 0: cFiles(2) = 0: cFiles(3) = 0
            cFolders(1) = 0: cFolders(2) = 0: cFolders(3) = 0
            Set FSO = CreateObject("SCripting.FileSystemObject")
            sFolder = "C:\myTest"
            SelectFiles sFolder
            MsgBox "# of files: " & cFiles(1) & vbNewLine & _
            "# of folders with files: " & cFiles(2) & vbNewLine & _
            "# of folders with no files " & cFiles(3) & vbNewLine & _
            "# of folders: " & cFolders(1) & vbNewLine & _
            "# of folders with subfolders: " & cFolders(2) & vbNewLine & _
            "# of folders with no subfolders: " & cFolders(3)
            Set FSO = Nothing

            End Sub

            '-----------------------------------------------------------------------
            Sub SelectFiles(Optional sPath As String)
            '-----------------------------------------------------------------------
            Dim oSubFolder As Object
            Dim oFolder As Object

            If FSO Is Nothing Then
            End If

            Set oFolder = FSO.GetFolder(sPath)
            cFiles(1) = cFiles(1) + oFolder.Files.Count
            If oFolder.Files.Count > 0 Then
            cFiles(2) = cFiles(2) + 1
            Else
            cFiles(3) = cFiles(3) + 1
            End If

            cFolders(1) = cFolders(1) + 1
            If oFolder.SubFolders.Count > 0 Then
            cFolders(2) = cFolders(2) + 1
            For Each oSubFolder In oFolder.SubFolders
            SelectFiles oSubFolder.Path
            Next
            Else
            cFolders(3) = cFolders(3) + 1
            End If


            End Sub
            HTH

            Bob

            Comment


            • #7


              Re: Count the number of subfolders within a folder

              Thanks Bob

              For some reason, I wasn't notified of your message in my email, so I didn't see it until just now. I actually used a recursive technique similar to the one that you gave me with in another thread the other day, and it seems to work:

              Code:
              'The following function will count the number of files stored within all
              'subfolders of the main root folder
              Public Function countFiles(strRootFolder) As Integer
              
                  Dim oFSO As Object                                              
                  Dim folder As Object                                           
                  Dim subfolders As Object                                     
                  Dim fldr
                   
                  Set oFSO = CreateObject("Scripting.FileSystemObject")
                  Set folder = oFSO.GetFolder(strRootFolder)
                  Set subfolders = folder.subfolders
                   
                  For Each fldr In subfolders
                      countFiles = countFiles + countFiles(fldr.Path)
                  Next
                  
                  countFiles = countFiles + folder.Files.Count
                   
                  Set oFSO = Nothing
                  Set folder = Nothing
                  Set subfolders = Nothing
              
              End Function
              Thanks for everything!
              Bryan

              Comment

              Working...
              X