I want to search an entire directory for a string, does anyone have a suggestion on how to do this in VB. The directory may contain RTF, Word, Excel, etc. documents. Thanks.;)
Printable View
I want to search an entire directory for a string, does anyone have a suggestion on how to do this in VB. The directory may contain RTF, Word, Excel, etc. documents. Thanks.;)
You can use the Dir function to retrieve all file names from a directory, and process them.
Then, you can define a ProcessFile sub which will, well, process the file. :rolleyes:Code:Dim sFileName As String
sFileName = Dir("C:\My Documents\")
Do Until sFileName = vbNullString
Call ProcessFile(sFileName)
sFileName = Dir
Loop
Enjoy! :rolleyes:Code:Sub ProcessFile(ByVal sFileName As String)
Dim sFile As String
Dim btFileNum As Byte
btFileNum = FreeFile
Open sFileName For Input As btFileNum
sFile = Input(LOF(btFileNum), btFileNum)
Close btFileNum
' Replace :rolleyes: with the string you want to search in the files... This will do a case-insensitive search
If InStr(1, sFile, ":rolleyes:", vbTextCompare) Then
' Whatever you want to do with it, if it contains the string...
Call lstGoodFiles.AddItem(sFileName)
Else
' Whatever you want to do with it, if it doesn't contain the string... (If anything)
Call lstBadFiles.AddItem(sFileName)
End If
End Sub
Tried it and it works with a modification. Must make sure that the directory name is passed along with the filename. Thanks a lot!!!