Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 198
  • Last Modified:

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?
0
cjinsocal581
Asked:
cjinsocal581
  • 6
  • 4
  • 3
1 Solution
 
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Fernando SotoCommented:
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
 
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 SotoCommented:
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 SotoCommented:
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 SotoCommented:
The Author may award point to those Experts fully answering the question.
0
 
Maxim10553Commented:
okay.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 6
  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now