Results 1 to 4 of 4

Thread: Count The Folders On a Hard Drive

  1. #1

    Thread Starter
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    What is the quickest way to count the total numbers of folders on a hard drive? If my drive X had the following structure, I'd expect the result to be 3; 1 for "Folder1", 1 for "Folder2" and 1 for "stuff".

    x:\Folder1
    x:\Folder2\stuff

  2. #2
    Lively Member
    Join Date
    Aug 2000
    Posts
    125
    seems like you have to use the findfirstfile findnextfile findclose API's in a recursive manner and count the directories - not counting "." and ".." in each directory

    a recursive sub to modify is at:
    http://pages.about.com/vbmakai/getfiles.htm

  3. #3
    Guest
    Here you go:

    Code:
    Sub DirMap(ByVal Path As String, List As ListBox)
        On Error Resume Next
        Dim i, j, x As Integer 'All used as counters
        Dim Fname(), CurrentFolder, Temp As String
        Temp = Path
        If Dir(Temp, vbDirectory) = "" Then Exit Sub 'if there arent any sub directories the exit
        CurrentFolder = Dir(Temp, vbDirectory)
    
        Do While CurrentFolder <> ""
    
    
            If GetAttr(Temp & CurrentFolder) = vbDirectory Then
    
    
                If CurrentFolder <> "." And CurrentFolder <> ".." Then
                    i = i + 1
                End If
            End If
            CurrentFolder = Dir
        Loop
        ReDim Fname(i) 'Redim the array With number of folders
        'now store the folder names
        CurrentFolder = Dir(Temp, vbDirectory)
    
    
        Do While CurrentFolder <> ""
    
    
            If GetAttr(Temp & CurrentFolder) = vbDirectory Then
    
    
                If CurrentFolder <> "." And CurrentFolder <> ".." Then
                    j = j + 1
                    Fname(j) = CurrentFolder
                    List.AddItem Temp & Fname(j)
                End If
            End If
            CurrentFolder = Dir
        Loop
    
    
        For x = 1 To i
            Call DirMap(Temp & Fname(x) & "\", List)
        Next
    End Sub
    
    
    
    Usage
    
    Private Sub Command1_Click()
    
         Call DirMap("C:\", List1)
         DoEvents
         Msgbox List1.ListCount & " - Folders on C drive."
    
    End Sub

  4. #4
    Guest
    What is the quickest way to count the total numbers of folders on a hard drive?
    Forget my way then, Martin, it's not slow, but it's not fast either .

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width