Using Nasted DIr

  • Thread starter Thread starter Ken Snell [MVP]
  • Start date Start date
K

Ken Snell [MVP]

No, nested Dir functions cannot be used. Dir can be used recursively, as
you've noted. In order to do what you want, you will need to build arrays of
the folders or files in a folder, and loop through the array using the Dir
function.
 
Hello there

Hello there

I need to build program that search files in Directory, including its sub
directories

For this i thought to use Dir

The first Dir("*.mdb") returns the first MDB file and the dir() returns the
next MDB file until the Dir return ""

This works fine if i work on only one directory
But if i need to search in subDirectory I need dir() for sub dircetories and
Dir inside the directory (Nasted Dir)

And it don't work

Is there a way to use Nasted dir? or if not is there another way to recive
all the MDB Files that I have in some Directory

I ask this question before and someone gave me good answer about function
with racursive

His replay has suddinly deleted

Whay is that?

and can i get the old respond back?


--
øåòé âåìãäîø
ù.î. òúéã äðãñú úåëðä
(e-mail address removed)
èì: 03-5611606
ôìà' 050-7709399
 
Here is a nice short routine that will "walk" the dir structure...

Sub dirTest()

Dim dlist As New Collection
Dim startDir As String
Dim i As Integer

startDir = "C:\access\"
Call FillDir(startDir, dlist)

MsgBox "there are " & dlist.Count & " in the dir"

' lets printout the stuff into debug window for a test

For i = 1 To dlist.Count
Debug.Print dlist(i)
Next i

End Sub


Sub FillDir(startDir As String, dlist As Collection)

' build up a list of files, and then
' add add to this list, any additinal
' folders

Dim strTemp As String
Dim colFolders As New Collection
Dim vFolderName As Variant

strTemp = Dir(startDir)

Do While strTemp <> ""
dlist.Add startDir & strTemp
strTemp = Dir
Loop

' now build a list of additional folders
strTemp = Dir(startDir & "*.", vbDirectory)

Do While strTemp <> ""
If (strTemp <> ".") And (strTemp <> "..") Then
colFolders.Add strTemp
End If
strTemp = Dir
Loop

' now process each folder (recursion)
For Each vFolderName In colFolders
Call FillDir(startDir & vFolderName & "\", dlist)
Next vFolderName

End Sub
 
Back
Top