RE: How do I search for a directory?

Tech-Archive recommends: Fix windows errors by optimizing your registry



Thanks Tom. Of the three ways suggested by Doug Steele I used the File
System Object. I needed to modify Doug's code a bit to find all files within
a particular directory name. For everyone's use, I'm including the code
below. The routine DirTest is a driver routine.

Public Sub FindFilesUsingFSO( _
StartDir As String, _
DirName As String, _
FileList As Collection _
)

Dim objFSO As Object ' FileSystemObject
Dim objFile As Object ' File
Dim objFolder As Object ' Folder
Dim objRootFolder As Object ' Folder

Set objFSO = _
CreateObject("Scripting.FileSystemObject")
Set objRootFolder = objFSO.GetFolder(StartDir)
If objRootFolder.Name = DirName Then
For Each objFile In objRootFolder.Files
With objFile
FileList.Add objRootFolder.path & "\" & .Name ' Collect all the
files
End With
Next objFile
End If

For Each objFolder In objRootFolder.SubFolders
With objFolder
Call FindFilesUsingFSO( _
objRootFolder.path & "\" & .Name, _
DirName, _
FileList _
)
End With
Next objFolder

End Sub

Public Sub dirTest(strStartDir As String, DirName As String)
Dim colFileList As Collection
Dim intLoop As Integer
Dim strMessage As String
Dim varFile As Variant


Set colFileList = New Collection
Call FindFilesUsingFSO( _
strStartDir, DirName, colFileList)
If colFileList.Count = 1 Then
strMessage = "There is 1 file under "
Else
strMessage = "There are " & _
colFileList.Count & " files under "
End If
strMessage = strMessage & strStartDir
Debug.Print strMessage
For Each varFile In colFileList
Debug.Print varFile
Next varFile
Debug.Print
Set colFileList = Nothing

End Sub




"Tom Wickerath" wrote:

Hi Leif,

Try this free article. It likely has what you need:

http://advisor.com/doc/16279


Tom Wickerath
Microsoft Access MVP

http://www.access.qbuilt.com/html/expert_contributors.html
http://www.access.qbuilt.com/html/search.html
__________________________________________

"Leif" wrote:

Does anyone know how to search for a particular directory name? I have a
large directory structure. I'm looking for a directory with the name of
"Multipgs". It will appear many times in the directory structure, even at
different levels. Once I find the directory I will record the directory path
in a table for later processing.

Thanks for your help.

Regards,
Leif
.



Relevant Pages