Scanning a text file for a specific keyword 
Author Message
 Scanning a text file for a specific keyword

Hi, I'm a VB newbie and need help with what is probably a very simple piece
of code.  I need to open a .txt file, scan for specific keyword, then
present the user with a message based on whether or not the keyword is
contained in the text file.

Thanks in advance for any help!
Jeanne



Mon, 23 Feb 2004 02:33:17 GMT  
 Scanning a text file for a specific keyword
Jeanne,
In the Project References, set a reference to "MIcrosoft Scripting Runtime"
and paste in the following function:
Private Function SearchFile(ByVal istrFile As String, _
                            ByVal istrKeyWord As String, _
                            ByRef ointLine As Integer, _
                            ByRef ointPosition As Integer, _
                            ByRef olngStatus As Long) As Boolean
'*********************************************************************
'Purpose:   To test for existence of a keyword in a text file
'Params:    istrFile:   The file to search with full path
'           istrKeyword: The keyword to find
'           ointLine:   Return the line of the file on which the keyword is
found
'           ointPosition: Return the position on the line
'           olngStatus: Return the error code or ZERO on success
'**********************************************************************
On Error GoTo errHandler
    Dim fso As New FileSystemObject
    Dim ts As TextStream
    Dim strText As String
    Dim intPosition As Integer
    Dim intLine As Integer

    Set ts = fso.OpenTextFile(istrFile, ForReading, False, TristateFalse)

    Do While Not ts.AtEndOfStream
        intLine = intLine + 1
        strText = ts.ReadLine
        intPosition = InStr(1, strText, istrKeyWord)
        If intPosition > 0 Then
            Exit Do
        End If
    Loop

    SearchFile = True
    ointPosition = intPosition
    ointLine = intLine
    olngStatus = 0
    GoTo CleanUp
errHandler:
    SearchFile = False
    olngStatus = Err.Number

CleanUp:
    If Not fso Is Nothing Then
        Set fso = Nothing
    End If

End Function

Call this function as follows:
Private Sub Command1_Click()
    Dim booresult As Boolean
    Dim intLine As Integer
    Dim intPosition As Integer
    Dim lngStatus As Long

    booresult = SearchFile("c:\autoexec.bat", "Processed", intLine,
intPosition, lngStatus)

    If booresult And intLine > 0 Then
        Text1.Text = "Keyword found on line " & intLine & ", position " &
intPosition
    ElseIf Not booresult Then
        Text1.Text = "Error: " & Err.Number
    Else
        Text1.Text = "Keyword not found"
    End If
End Sub

If you need any assistance understanding the code please let me know.

Create a FileSystemObject and pass the file name to the

Quote:
> Hi, I'm a VB newbie and need help with what is probably a very simple
piece
> of code.  I need to open a .txt file, scan for specific keyword, then
> present the user with a message based on whether or not the keyword is
> contained in the text file.

> Thanks in advance for any help!
> Jeanne



Wed, 25 Feb 2004 13:08:21 GMT  
 Scanning a text file for a specific keyword
Mike, thanks so much for your help!  I had a few small problems, but managed to make it work.  

I wanted to use this code for another small project as well, but can't seem to make things work correctly.  I'd like to be able to replace the found text with user input.  The problem that I'm having, is that when the text file is opened, everything is deleted.  I assumed this was because I changed the ForReading to ForWriting.  However, the replacement does not work either way.

Again, I can't thank you enough for the help you've given me.  You've saved me days of head-banging frustration!
Jeanne

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!



Sat, 28 Feb 2004 00:24:10 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Keyword Search on MS Word and Text Files

2. Keyword Search on MS Word and Text Files

3. provider-specific keywords

4. --> Scan the content of a text file

5. how to insert a text-file via macro with a specific font

6. Reference/Pointer to a specific line in a text file

7. QBasic: Reading specific data from a text file?

8. Read from Specific String and write to Text File

9. Reading a specific line from a text file

10. write to a specific line in a text file

11. Entering text into a text box at a specific point

12. getting a specific line of text from a text box (vb5)

 

 
Powered by phpBB® Forum Software