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!

File Searches...

Status
Not open for further replies.

cwalshe

Programmer
May 28, 2001
84
IE
Ok, so I'm not sure if anyone will be able to help here or not but I am looking for code that will allow me to search through the contents of files in a drive or directory for a string.

Thx,

Cormac.
 
There is probably a better way to do this, but this is how I do it:

Sub SearchFiles(NamePart As String, FolderPath As String, Files() As String)
Dim objFS
Dim objFolder
Dim File
Dim k As Integer

Set objFS = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFS.GetFolder(FolderPath)
For Each File In objFolder.Files
If InStr(File.Name, NamePart) > 0 Then
k = k + 1
ReDim Preserve Files(k)
Files(k) = File.Name
End If
Next

Set objFolder = Nothing
Set objFS = Nothing

End Sub

Here is a sample Form:

Private Sub Command1_Click()
Dim FileList() As String
Dim k As Integer

Call SearchFiles(TxtInput, txtFolder, FileList())


For k = LBound(FileList()) To UBound(FileList())
TxtOutput = TxtOutput & FileList(k) & vbCrLf

Next

End Sub

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top