Repost: Find files containing certain keywords without FileSearch?

E

Ed from AZ

Now that Application.FileSearch has been killed off in the 2007
versions, is there a function to use to find a file containing
certain keywords?

Ed
 
H

Helmut Weber

Hi Ed,

as it seems, nobody has a better idea,
the fastest and most unreliable way,
which searches unfortunately for one string only,
is to use the DOS-command find, like

C:\Test>find /C "JOHN BROWN" *.doc > c:\test\result.txt

and evaluate result.txt, which looks like that:
---------- AND.DOC: 0
---------- DOC-001.DOC: 0
---------- DOC-002.DOC: 0
---------- DOC-004.DOC: 0
---------- PAGE-03.DOC: 0
---------- PAGE-04.DOC: 0
---------- SPONSOR.DOC: 1

"JOHN BROWN" is found in "SPONSOR.DOC"

See help on find:

C:\Test>find /?


--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 
E

Ed from AZ

Hi, Helmut. Thanks for the response.

The docs I would be searching are text-only, and usually not more than
four pages. There may be a couple thousand of them, though. The text
searches I do are usually for two or three separate words, not a
single string. I wonder if it would be too time-consuming to iterate
through all the docs in a single folder, read the contents into a
string, and check each string for the words. I guess that means each
doc would have to be opened to read the contents.

Otherwise, I would have to perform the DOS FIND search three separate
times, and find a way to read the results file to find which, if any,
docs had a match across all three words.

Ed
 
H

Helmut Weber

Hi Ed,
I wonder if it would be too time-consuming to iterate
through all the docs in a single folder, read the contents into a
string, and check each string for the words. I guess that means each
doc would have to be opened to read the contents.
sure.
About processing time, You won't know before you've tried it out.
Otherwise, I would have to perform the DOS FIND search three separate
times, and find a way to read the results file to find which, if any,
docs had a match across all three words.
Evaluation would be too complicated.






--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 
K

Ken

Ed

The following VBA code will search every .txt and .doc file in a
folder for any number of key words or search strings. The code
searches in the current folder but you could put any folder name into
pathStr.

Option Explicit
Sub FolderSearch()
Dim fs As Object
Dim objFolder As Object
Dim pathStr As String
Dim fleObj As Object
Dim fName As String

pathStr = ActiveDocument.Path

Set fs = CreateObject("Scripting.FileSystemObject")
Set objFolder = fs.getfolder(pathStr)
For Each fleObj In objFolder.Files
fName = fleObj.Name
If isTextFile(fName) Then
If CheckForStrings(fName) Then
MsgBox "Keyword found in file " & fName
End If
End If
Next fleObj
End Sub

Function isTextFile(str As String) As Boolean
' only search .txt and .doc files.
If InStr(str, ".txt") Or InStr(str, ".doc") > 0 Then
isTextFile = True
Else
isTextFile = False
End If
End Function

Function CheckForStrings(fName As String)
Dim searchStrings()
Dim i As Integer
Dim rr As Range

searchStrings = Array("String1", "String2", "etc.") ' strings to
search for

If ThisDocument.Name = fName Then
CheckForStrings = False
Exit Function
End If
Documents.Open FileName:=fName
For i = 0 To UBound(searchStrings)
With Documents(fName)
Set rr = .Content
rr.Find.Execute findtext:=searchStrings(i)
If rr = searchStrings(i) Then
CheckForStrings = True
GoTo closef
End If
End With
Next i
CheckForStrings = False
closef:
Documents(fName).Close SaveChanges:=wdDoNotSaveChanges
End Function

Ken
 
K

Ken

Ed

The above code will find files that contain any of the search strings.
If you want to find files that contain all of the search strings then
the subroutine CheckForStrings becomes:

Function CheckForStrings(fName As String)
Dim searchStrings()
Dim i As Integer
Dim rr As Range

searchStrings = Array("String1", "String2", "etc.") ' strings to
search for

If ThisDocument.Name = fName Then
CheckForStrings = False
Exit Function
End If
Documents.Open FileName:=fName
CheckForStrings = True
For i = 0 To UBound(searchStrings)
With Documents(fName)
Set rr = .Content
rr.Find.Execute findtext:=searchStrings(i)
If rr <> searchStrings(i) Then
CheckForStrings = False
End If
End With
Next i
Documents(fName).Close SaveChanges:=wdDoNotSaveChanges
End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top