Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


Getting directory list...

Posted on 1999-07-19
Medium Priority
Last Modified: 2010-04-30
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,

Question by:mikemonnex
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Expert Comment

ID: 1525697
Use FindFirstFile, FindNextFile in recursive function

Author Comment

ID: 1525698
I would need some code or pseudo code.  THis is an API function, right?  If so, how do I implement it?


Accepted Solution

Vbmaster earned 400 total points
ID: 1525699
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.


Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

704 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question