|
-
Sep 28th, 2000, 10:24 AM
#1
Thread Starter
Member
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.
-
Sep 28th, 2000, 12:54 PM
#2
Guru
You can use the Dir function to retrieve all file names from a directory, and process them.
Code:
Dim sFileName As String
sFileName = Dir("C:\My Documents\")
Do Until sFileName = vbNullString
Call ProcessFile(sFileName)
sFileName = Dir
Loop
Then, you can define a ProcessFile sub which will, well, process the file. 
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
Enjoy!
-
Sep 28th, 2000, 03:07 PM
#3
Thread Starter
Member
Thanks!
Tried it and it works with a modification. Must make sure that the directory name is passed along with the filename. Thanks a lot!!!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|