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

x
?
Solved

Getting directory list...

Posted on 1999-07-19
3
Medium Priority
?
143 Views
Last Modified: 2010-04-30
Hi, let's say I have the following structure:

C:\
   Mike\
        abc
        012
        xyz
        .
        .
        .

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,


Mike.
0
Comment
Question by:mikemonnex
[X]
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
3 Comments
 
LVL 6

Expert Comment

by:setiawan
ID: 1525697
Use FindFirstFile, FindNextFile in recursive function
0
 

Author Comment

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

0
 
LVL 7

Accepted Solution

by:
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
  Loop
 
  If Recursive Then
    For a = Dirstart To Dircount
      If Not ListDirs(Dirs(a), Dirs, Recursive, Dircount) Then
        ListDirs = False
        Exit Function
      End If
    Next
  End If
  ListDirs = True
  Exit Function
 
ErrorHandler:
  '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.

0

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