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!

choose directory and all subdirectories

Status
Not open for further replies.

sublimo

Programmer
Nov 25, 2001
6
US
I have a form where the user has to choose a directory. The user has to give the first letter of a filename, and the form returns all files in this directory that start with this letter.
I have found code for a browsefolder that enables you to choose a specific directory.
BUT, the form should not just look for the files in the chosen directory, but also in its subdirectories!!

anyone an idea how to check the subdirectories also?

Thanks
 
Hey, make a reference to Scripting.FileSystemObject. It provides almost all the method you need to access the files in your hard drive. I know you've already done some of this, but once you make the reference, and try it out, you'll see that it's probably easier to do it this way. And as to your question, I believe that something like :
'Set aFolder = .GetFolder(<Path>).Subfolders' is what you need. Check it out, maybe there are other ways to do this, but this is the only way I know, and I think it's quite useful. Hope this helps a little bit.
--Merlin
 
Private Sub LoopThruFolders(strPath as String, _
strMatch as string)
Dim fso as FileSystemObject
Dim fdrParent as Folder
Dim fdrSub as Folder
Dim fil as File

Set fso = New FileSystemObject
Set fdrParent = fso.GetFolder(strPath)

For each fil in fdrParent.Files
.
<<Work with files here>>
e.g. If fil.name like (strmatch & &quot;*&quot;) then
Debug.Print fil.name
End If
.
Next fil

For each fdrSub in fdrParent.SubFolders
LoopThruFolders(fdrSub.Path, strMatch)
Next fdrSub

Set fdrParent = nothing
Set fso = nothing

End Sub

It's been a while since I've done this. As long as you reference the MS Scripting library it should give you a start, but I'm bound to have forgotten/typoed something.

Andy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top