find words in textfiles from access table

C

Co

Hi All,
I took this post away from the vb newsgroups.
I created a code that scans through textfiles in a given folder using
findstr.
I got so far as this where I give a string to search for and get a
notification when the word was found.
Now I want this code to loop through a recordset with words and use a
recordset with the filenames of the
files in my folder.

Lets say I have a recordset (rst1) with the words to search for (tree,
flower, earth, water)
And a recordset (rst2) holding the filenames of the files
(mytestdoc1.txt, mytestdoc2.txt, mytestdoc3.txt)

I want to start with "tree" and check all the textfiles. When a match
is found rst2 is opened and the record
that holds the filename the match was found in is updated with the
found word.
So for example in the record with mytestdoc2.txt the word "tree"is
added in the field "Keywords".

Can anyone figure out how to do this?


Option Compare Database
Option Explicit
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As
Long)
Public Function testme03()

Dim myCmd As String
Dim mySearchFileName As String
Dim myResultFileName As String
Dim myLine As String
Dim stringToFind As String
Dim myFileNum As Long
Dim FoundIt As Boolean
Dim Str As String
Dim cnt As Long
Dim TmpFileName As String
Dim TmpFileName2 As String

mySearchFileName _
= "d:\*.txt"
myResultFileName _
= "d:\" & Format(Now, "yyyymmdd_hhmmss") & ".txt"
stringToFind = "*Box"

If Dir(mySearchFileName) = "" Then
MsgBox mySearchFileName & " doesn't exist"
Exit Function
End If

If stringToFind Like "*[?*]*" Then
Str = Replace(stringToFind, "?", ".")
Str = Replace(stringToFind, "*", ".*")
Else
Str = "\<" & stringToFind & "\>" 'an exact match
End If

' Build console command (/i = case insensative, /m = List file
names)
myCmd = Environ("comspec") & " /c findstr /c /i /m """ & Str & """
"""
myCmd = myCmd & mySearchFileName & """ "
myCmd = myCmd & "> " & myResultFileName

Debug.Print myCmd
Shell myCmd

'Pause long enough for the Find to work.
Sleep 250

On Error GoTo Sleeper
Sleeper:
' Limit recursion
cnt = cnt + 1
If cnt > 100 Then
MsgBox "Too many to list...."
Exit Function
End If

' DoEvents

myFileNum = FreeFile()
Close #myFileNum
Open myResultFileName For Input As #myFileNum

FoundIt = False
Do While Not EOF(myFileNum)
Line Input #myFileNum, myLine
If myLine <> myResultFileName Then
Debug.Print myLine
End If
If Len(Trim(myLine)) > 0 Then
If Right(myLine, 2) <> " 0" Then
FoundIt = True
'Exit Do
End If
End If
volgende:
Loop
Close #myFileNum
Sleep 50
Kill myResultFileName

If FoundIt Then
MsgBox "found it"
Else
MsgBox "nope"
End If

End Function

Regards
Marco
The Netherlands
 

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