Hopefully someone can tell me how do I do a search for all files with a .txt extension within a directory and it's subdirectories.
Thanks a lot for your help
JK
Printable View
Hopefully someone can tell me how do I do a search for all files with a .txt extension within a directory and it's subdirectories.
Thanks a lot for your help
JK
Use a filelistbox - you can set the its path and pattern
fillb.path = "root directory"
fillb.pattern= "*.txt"
I'm not too sure if it will look in the subdirectories as well...
H.
Code:Option Explicit
Private Function GetFiles(sPath As String)
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Dim sFilename As String
Dim fld
If fso.FolderExists(sPath) Then
sPath = sPath & IIf(Right$(sPath, 1) <> "\", "\", "")
sFilename = Dir(sPath & "*.txt")
Do While Len(sFilename)
MsgBox sPath & sFilename
sFilename = Dir
Loop
For Each fld In fso.GetFolder(sPath).SubFolders
GetFiles fld.Path
Next
End If
End Function
Private Sub Command1_Click()
Call GetFiles("C:\my documents")
End Sub