Button to search all TXT files for keywords function.

Here is what I am trying to accomplish with little success.

I need to add a button to my existing program that searches through the existing text files that are within a specified folder for additional keywords that I specify into a text box seperated by commas.

Then, when the search file finds the keywords, it will add each instance to a listbox with the name of the file and the line number it found the instance on.

I currenly monitor only new files and the string I have for the keywords is within a Private declaration and I am struggling to get them both to work.

Any ideas?
LVL 6
cjinsocal581Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Maxim10553Commented:
homework?
0
cjinsocal581Author Commented:
No, this is for a monitor program that I wrote to check files that are being sent from a faulty system.
0
Maxim10553Commented:
okay, what do you have so far? You can either use the file system object or .net's my object to loop through the file directory.  
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

Fernando SotoRetiredCommented:
Hi cjinsocal581;

Here is some code using Regular Expressions to do what you need. The code assumes that you have the following controls:

A TextBox called SearchDir = Directory to search in
A TextBox called SearchFor = Search words to look for
A ListBox called Results = To hold the information
A Button called Start to execute the below code.

    Private Sub Start_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Start.Click

        ' Check to see if the search directory and search word text
        ' boxes are filled if not exit
        If SearchDir.Text = "" Or SearchFor.Text = "" Then Return

        ' Check to see if the direcory exists
        If Directory.Exists(SearchDir.Text) Then
            ' Clear the list box
            Results.Items.Clear()
            ' Remove any spaces at the begining and end of the string
            Dim pattern As String = SearchFor.Text.Trim()
            ' Remove the commas and spaces from the search string
            pattern = Regex.Replace(pattern, "\s*,\s*", "|")
            ' Remove the last | if one is there
            If pattern.Substring(pattern.Length - 1) = "|" Then
                pattern = pattern.Substring(0, pattern.Length - 1)
            End If
            ' Set up the Regex search pattern
            pattern = "^(.*?\b(" & pattern & ")\b.*?)+?\n|.*?\n|\n$"
            ' Regex options to use on the Regex search
            Dim reOptions As RegexOptions = RegexOptions.IgnoreCase Or _
                RegexOptions.Multiline Or RegexOptions.Compiled
            ' Create a Regex Object with pattern and options
            Dim re As New Regex(pattern, reOptions)
            ' Get directory info and files
            Dim dInfo As New DirectoryInfo(SearchDir.Text)
            ' Change the search pattern to get files you need
            Dim fInfo() As FileInfo = dInfo.GetFiles("*.txt")

            ' Go through each file one by one
            For Each f As FileInfo In fInfo
                ' The line number the match was found on
                Dim lineNum As Integer = 0
                ' Read the whole file into the input variable and close the file
                Dim sr As New StreamReader(f.FullName)
                Dim input As String = sr.ReadToEnd()
                sr.Close()
                ' Get all the matches in the input file
                Dim mc As MatchCollection
                mc = re.Matches(input)
                ' Go through all the matches and add it to the list box
                For Each m As Match In mc
                    lineNum += 1
                    If Not m.Groups(2).Value = "" Then
                        Dim word As String = m.Groups(2).Value()
                        Results.Items.Add(f.Name & " Word found is " & word & _
                            " On line " & lineNum)
                    End If
                Next
            Next

        End If

    End Sub

I hope that this is of some help.

Fernando
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
cjinsocal581Author Commented:
FernandoSoto:

thanks again, this will work hand in hand with the code you helped me with on the monitor.

cheers!

CJ
0
Fernando SotoRetiredCommented:
Glad I was able to help. :=)
0
cjinsocal581Author Commented:
Do you think a progressbar would cause to much cpu time if I added one?
0
Maxim10553Commented:
was i a big help?
0
Fernando SotoRetiredCommented:
Hi cjinsocal581;

I do not think adding a progress bar would cause much CPU time. If you add a progress bar add a Application.DoEvents() statement in the inner For loop like this:

                For Each m As Match In mc
                    lineNum += 1
                    If Not m.Groups(2).Value = "" Then
                        Dim word As String = m.Groups(2).Value()
                        Results.Items.Add(f.Name & " Word found is " & word & _
                            " On line " & lineNum)
                     Application.DoEvents()
                End If

This will handle the progress bar events to be processed while that part of the code is executing.

Fernando.
0
Maxim10553Commented:
or you could just code the progress bar in a seperate thread
0
Maxim10553Commented:
where are my points?
0
Fernando SotoRetiredCommented:
The Author may award point to those Experts fully answering the question.
0
Maxim10553Commented:
okay.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.