Getting directory list...

Hi, let's say I have the following structure:


Basically, there is one directory on the hard drive that has multiple subdirectories.  I would like to read in all those subdirectories and put them into an array as strings.

Does anybody have code to accomplish this?

Thank you very much,

Who is Participating?
VbmasterConnect With a Mentor Commented:
Here's the code for it..

Function ListDirs(ByVal Path As String, ByRef Dirs() As String, Recursive As Boolean, Optional Dircount As Double = 0) as Boolean

  Dim Dirname   As String
  Dim Dirstart  As Long
  Dim a         As Long
  On Error GoTo ErrorHandler
  Dirstart = Dircount + 1
  If (Dircount = 0) Then
    ReDim Dirs(0)
    Path = IIf(Right$(Path, 1) = "\", Path, Path + "\")
  End If
  Dirname = Dir$(Path + "*.*", vbDirectory)
  Do While (Dirname <> "")
    If (Dirname <> ".") And (Dirname <> "..") Then
      If ((GetAttr(Path & Dirname) And vbDirectory) = vbDirectory) Then
        Dircount = Dircount + 1
        ReDim Preserve Dirs(Dircount)
        Dirs(Dircount) = Path + Dirname + "\"
      End If
    End If
    Dirname = Dir
  If Recursive Then
    For a = Dirstart To Dircount
      If Not ListDirs(Dirs(a), Dirs, Recursive, Dircount) Then
        ListDirs = False
        Exit Function
      End If
  End If
  ListDirs = True
  Exit Function
  'Any error message(s) can be placed here
  ListDirs = False
End Function

This function will return True if everything went okay, and False if YOU did something wrong. What you enter the function is

rValue = ListDirs(--Path to search--, --the string array (dynamic!)--, --do you want to recursively search this path?--)

The third parameter is only used in recursive calling so you don't do anything with it.

Use FindFirstFile, FindNextFile in recursive function
mikemonnexAuthor Commented:
I would need some code or pseudo code.  THis is an API function, right?  If so, how do I implement it?

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.