Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

recursion problem

Status
Not open for further replies.

rewdee

Programmer
Aug 17, 2001
295
US
I am trying to list all the folders in a particular path subfolders. Unfortunately, I have a problem when the recursion gets to the last folder. The next dir returns an invalid procedure error. Here's the code:
Code:
Sub FindFolders(sFileStart As String)
Dim MyPath As String, MyName As String

MyPath = sFileStart  MyName = Dir(MyPath, vbDirectory)   Do While MyName <> &quot;&quot;  
    If MyName <> &quot;.&quot; And MyName <> &quot;..&quot; Then 
        If (GetAttr(MyPath & MyName) And vbDirectory) = vbDirectory Then
            Debug.Print MyName  
            FindFolders MyPath & MyName & &quot;\&quot;
        End If  
    End If
    MyName = Dir 
Loop
End Sub

Sub test()
  FindFolders &quot;C:\My Documents\&quot;
End Sub

Can anyone see what the problem is or share some code that can help.

Thanks,
Rewdee
 
This method isn't going to return all the folders in a path even if it didn't get killed when you call dir with no arguments and it has already reached the last entity in the path. It says in the help files that you can't call dir recursively and although I certainly applaud your attempts to prove that wrong, I can't think of any way to do it.
JHall
 
Just found this on the web.

Sub DirSearch(ByVal StartDir As String)
Dim s As String
Dim currdir As String
Dim dirlist As New Collection

If Right$(StartDir, 1) <> &quot;\&quot; Then StartDir = StartDir & &quot;\&quot;
dirlist.Add StartDir
While dirlist.Count
'remove current directory from directory list
currdir = dirlist.Item(1)
dirlist.Remove 1
'find all files and subdirectories in current, add to list
s = Dir$(currdir, vbDirectory)
While Len(s)
If (s <> &quot;.&quot;) And (s <> &quot;..&quot;) Then 'get rid of &quot;.&quot; and &quot;..&quot;
If GetAttr(currdir & s) = vbDirectory Then 'add the subdirectory
dirlist.Add currdir & s & &quot;\&quot;
Else 'work on the file
Debug.Print currdir & s
End If
End If
s = Dir$
Wend
Wend
End Sub
JHall
 
Thanks for the response.

Unfortunately that code will only return the subfolders in the current directory. So if you have a structure like:

C:\My Documents
Delphi
Projects
Project 1
Project 2
Access
Projects
Project 1
Project 2
etc...

And the applied code will only return Delphi and Access but none of the subfolders.

It took me about a day and quite a few lines of code but I did work out the code to get the directory structure, files and file attributes for any root directories on any mask (I'm currently working on multiple masks). If anyone is interested just Email me and I will send the project.

Rewdee
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top