Bert Smets
February 10th, 2000, 10:54 AM
I'm writing a program to search a disk for files of a specified type (eg. *.bmp). I know how to search for the file in a directory I specify but i don't know how I can let the program also search all subdirectories. Is there anyone who can help me with this problem?
Bert
Johnny101
February 10th, 2000, 11:55 AM
Set a reference to the Microsoft Scripting Runtime and then use code similar to this to loop through all the subdirectories... Place a command button on a form and then paste this code:
option Explicit
Dim fso as Scripting.FileSystemObject
Dim ofolder as Scripting.folder
Dim subflder as Scripting.folder
Dim ofile as Scripting.File
private Sub Command1_Click()
set fso = new Scripting.FileSystemObject
set ofolder = fso.GetFolder("C:\")
me.Caption = "Searching... C:"
for Each subflder In ofolder.SubFolders
Call RecursiveSearch(subflder)
next
End Sub
Sub RecursiveSearch(folder as Scripting.folder)
Dim oFldr as Scripting.folder
me.Caption = "Searching..." & folder.Path
for Each oFldr In folder.SubFolders
If oFldr.SubFolders.Count then
RecursiveSearch oFldr
End If
for Each ofile In oFldr.Files
If Right(ofile.Name, 4) = ".bmp" then
MsgBox ofile.Name
End If
next
next
End Sub
this code will go through the entire drive and popup a message box with the name of every .bmp file. The searching routine probably isn't the best, but it does get you through every directory and every subdirectory.
Hope this helps,
John
John Pirkey
MCSD
www.ShallowWaterSystems.com