Getting Recursive Directory List... 
Author Message
 Getting Recursive Directory List...

Right now, all this does is get the directories in the path specified. How
would I modify this to get ALL recursive directory names? TIA!

    Dim DirNames(32768)
    Dim CountDirs As Long
    Dim DirName As String

    Directory = "E:\"
    If Right(Directory, 1) <> "\" Then Directory = Directory & "\"
    DirName = Dir(Directory & "*.*", 24)

    Do While DirName <> vbNullString
        If DirName <> "" And DirName <> ".." And DirName <> "." Then
            If (GetAttr(Directory & DirName) And vbDirectory) = vbDirectory
Then
                CountDirs = CountDirs + 1
                DirNames(CountDirs) = DirName
            End If
        End If
        DirName = Dir
    Loop

    Debug.Print CountDirs; "Directories"



Thu, 22 May 2003 03:00:00 GMT  
 Getting Recursive Directory List...
You'll need something more like this:

Option Explicit
Dim DirNames() As String

Private Sub Command1_Click()
Dim txt
  'Establish first call
  ReDim DirNames(0) As String
  'Identify the start directory
  DirNames(0) = "E:\"
  'Hop to it...
  GetDirectories

  'Lets see...
  Debug.Print UBound(DirNames) + 1
  For Each txt In DirNames
    Debug.Print txt
  Next

End Sub

Private Sub GetDirectories()
Static D&, DirCount&
Dim tmp$

   If UBound(DirNames) = 0 Then  'First call
     D = 0
     DirCount = 1
     ReDim Preserve DirNames(0 To 1023) As String
   Else
     D = D + 1
     If D >= DirCount Then Exit Sub
   End If
   If Right$(DirNames(D), 1) <> "\" Then
     DirNames(D) = DirNames(D) & "\"
   End If
   tmp = Dir(DirNames(D) & "*.*", 24)
   Do While tmp <> vbNullString
'Debug.Print DirNames(D) & tmp
     If tmp <> "" And tmp <> ".." And tmp <> "." Then
        If (GetAttr(DirNames(D) & tmp) And vbDirectory) = vbDirectory Then
           DirNames(DirCount) = DirNames(D) & tmp & "\"
           DirCount = DirCount + 1
           If DirCount > UBound(DirNames) Then
             ReDim Preserve DirNames(0 To DirCount + 1023) As String
           End If
        End If
     End If
     tmp = Dir()
   Loop
   GetDirectories
   If UBound(DirNames) <> DirCount - 1 Then ReDim Preserve DirNames(DirCount - 1) As String

End Sub

--

President - ChildsPlay Software Services

Quote:

> Right now, all this does is get the directories in the path specified. How
> would I modify this to get ALL recursive directory names? TIA!

>     Dim DirNames(32768)
>     Dim CountDirs As Long
>     Dim DirName As String

>     Directory = "E:\"
>     If Right(Directory, 1) <> "\" Then Directory = Directory & "\"
>     DirName = Dir(Directory & "*.*", 24)

>     Do While DirName <> vbNullString
>         If DirName <> "" And DirName <> ".." And DirName <> "." Then
>             If (GetAttr(Directory & DirName) And vbDirectory) = vbDirectory
> Then
>                 CountDirs = CountDirs + 1
>                 DirNames(CountDirs) = DirName
>             End If
>         End If
>         DirName = Dir
>     Loop

>     Debug.Print CountDirs; "Directories"



Thu, 22 May 2003 03:00:00 GMT  
 Getting Recursive Directory List...
An intermediate solution would be to use the FileSystemObject.

You can use a recursive folder scan routine.  This example fills a list
box with all the folders and sub-folders in a directory named "C:\WINDOWS"

'Add a project reference to "Microsoft Scripting Runtime"
'Set List1.Sorted = True

Option Explicit
Dim FSys As New FileSystemObject

Private Sub Command1_Click()
Dim FolderSpec As String

    FolderSpec = "C:\WINDOWS"
    List1.AddItem FolderSpec
    ScanFolder (FolderSpec)

End Sub

Public Sub ScanFolder(FolderSpec)
Dim ThisFolder As Folder
Dim allFolders As Folders

    Set ThisFolder = FSys.GetFolder(FolderSpec)
    Set allFolders = ThisFolder.SubFolders

    For Each ThisFolder In allFolders
        List1.AddItem ThisFolder.Path
        ScanFolder (ThisFolder.Path)
    Next

End Sub

Rob

Quote:

> Right now, all this does is get the directories in the path specified. How
> would I modify this to get ALL recursive directory names? TIA!

>     Dim DirNames(32768)
>     Dim CountDirs As Long
>     Dim DirName As String

>     Directory = "E:\"
>     If Right(Directory, 1) <> "\" Then Directory = Directory & "\"
>     DirName = Dir(Directory & "*.*", 24)

>     Do While DirName <> vbNullString
>         If DirName <> "" And DirName <> ".." And DirName <> "." Then
>             If (GetAttr(Directory & DirName) And vbDirectory) = vbDirectory
> Then
>                 CountDirs = CountDirs + 1
>                 DirNames(CountDirs) = DirName
>             End If
>         End If
>         DirName = Dir
>     Loop

>     Debug.Print CountDirs; "Directories"



Fri, 23 May 2003 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Recursive directory list

2. Recursive Directory Listing

3. getting list of directories into list box

4. Getting list of files in a directory

5. ?? Getting Directory Listing with qbasic

6. Getting a list of files in a directory

7. Getting directory list of a folder's contents

8. Getting a http directory list.

9. getting list of files in a directory

10. Getting Directory List

11. Getting a detailed directory listing using Internet transfer control, file sizes

12. Problem getting directory listing

 

 
Powered by phpBB® Forum Software