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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I get all the subdirs with the dir command 1

Status
Not open for further replies.

harmmeijer

Programmer
Mar 1, 2001
869
CN
I want all the directory names from c: (or any other location).
All the subdirectorys of these directorys need to be included.
In perl you can do something like the script below (function calling itselfe everytime a directory is encountered) but in access it does not work.
Is there something I can do because now I ame writing to a batchfile, running the batchfile and printing the output to a textfile and then import the textfile. I want to do this with the dir command.
Here is the code which I made and does not work

Public Function eentest(mypath As String)
' mypath has to be given when calling the function
' it has to end with the \ sign
If mypath <> &quot;&quot; Then
myname = Dir(mypath, vbDirectory)
Do While myname <> &quot;&quot; ' Start the loop.
' Ignore the current directory and the encompassing directory.
If myname <> &quot;.&quot; And myname <> &quot;..&quot; Then
' Use bitwise comparison to make sure MyName is a directory.
If (GetAttr(mypath & myname) And vbDirectory) = vbDirectory Then
eentest (mypath & myname & &quot;\&quot;)
Debug.Print myname
End If ' it represents a directory.
End If
myname = Dir ' Get next entry.
Loop
End If
End Function
 
When a function calls itself, that's called &quot;recursion&quot;. In general, VBA allows recursion, but unfortunately, the way Microsoft implemented the Dir() function doesn't allow recursion. That's why your code doesn't work.

But every solution implemented with a recursive function can also be implemented without recursion. One approach is to save each directory name in an array (ReDim the array each time, instead of guessing how big you'll need it to be). Even better, start out with the array containing your &quot;home&quot; directory (the one you want to start in). The main loop in your procedure walks through the array, with an inner loop getting all the file names and directory names for that array entry. When you've gotten everything from one directory, the inner loop ends and the outer loop starts over with the next array entry. When you've finished all the array entries, you will have generated all the path names. (Watch out for the &quot;.&quot; and &quot;..&quot; directories--you don't want to process them or you'll be in an infinite loop.)

Of course, they won't be in the same order as they would have with recursion. If that matters, you'll have to come up with a way to preserve the order. Maybe you could build a tree structure in another array, with each parent directory containing the index of its first child, and with an indicator for the last child in a directory. Once the tree is built, you can use a recursive function to walk the tree and output the names. Rick Sprague
 
How can I get the length of an array, I would like to try to put all directorys in an array but cannot find the statement like:
for each thing in myarray do
or:
while i1 < myarray.length do

This seens not to be supported by vba in access.
 
Sorry, allready got it:
hrm = Array(1, 33)
For Each dd In hrm
Debug.Print dd
Next
 
Sorry I noted your advice to be helpfull, I tried putting all the directorys in an array and get out of reach. Tried putting them in a temporary table and it takes forever.
On my machine there are about 2800 directorys imagene what it wil do on a server.
I wil stick with running the batchfile and import the result from the dir command IN THE BATCHFILE.
The dir command in vb is completely useless for getting all the directorys, both in vba as in visual basic.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top