dcsimg
Results 1 to 8 of 8
  1. #1

    Thread Starter
    New Member
    Join Date
    May 2017
    Posts
    12

    Finding all keywords in a text file

    Hi,

    Please help me figure out on how to get all the keywords with the same keyword "COMBINED WITH" and see the output on datagridview Thank you


    Code:
    Public Class Form1
        Private Sub ListFiles(ByVal folderPath As String)
         
            Dim fileNames = My.Computer.FileSystem.GetFiles(folderPath, FileIO.SearchOption.SearchTopLevelOnly, "*.txt")
    
            For Each fileName As String In fileNames
    
                DataGridView1.Rows.Add(New Object() {System.IO.Path.GetFileName(fileName)})
    
            Next
      
        End Sub
        Private Sub ListFiles2(ByVal folderPath1 As String)
            ' filesListBox.Items.Clear()
    
            Dim fileNames11 = My.Computer.FileSystem.GetDirectories(folderPath1)
    
    
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            If FolderBrowserDialog1.ShowDialog() = DialogResult.OK Then
                ' List files in the folder.
                ListFiles(FolderBrowserDialog1.SelectedPath)
                TextBox1.Text = (FolderBrowserDialog1.SelectedPath)
    
                For x As Integer = 0 To (Val(DataGridView1.Rows.Count) - 1)
                  
                    Dim filename As String = DataGridView1.Item(0, x).Value.ToString
                    Dim text As String = File.ReadAllText(FolderBrowserDialog1.SelectedPath + "\" + filename)
                    Dim index As Integer = text.IndexOf("COMBINED WITH")
    
    
    
                    If index >= 0 Then
                        Dim index1 = text.Substring(index, 60)
                          'Dim Findstring = File.ReadAllText(FolderBrowserDialog1.SelectedPath)
                        Dim Lookfor As String = index1
    
                        If text.Contains(index1) Then
                             DataGridView1.Rows.Add(New Object() {System.IO.Path.GetFileName(filename), Lookfor})
    
                        End If
    
                    End If
                Next
    
    
            End If
        End Sub

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    98,679

    Re: Finding all keywords in a text file

    What's the actual problem? You've posted some code with no explanation of why. Does it work or not? If not, what exactly happens and where does it happen? We shouldn't have to work it out from the code when you can just tell us.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    New Member
    Join Date
    May 2017
    Posts
    12

    Re: Finding all keywords in a text file

    Hi it works. But it only shows one "combined with " keyword in a text file i just want to see all keywords with the word "combined with" inside the a text file and make it appear on my datagridview

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    98,679

    Re: Finding all keywords in a text file

    Quote Originally Posted by lockwater13 View Post
    and make it appear on my datagridview
    What is "it"? Do you mean the index? Please provide a FULL and CLEAR explanation of the problem.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    Fanatic Member
    Join Date
    Dec 2014
    Location
    VB6 dinosaur land
    Posts
    815

    Re: Finding all keywords in a text file

    It appears you're adding a 60 character string to the DGV. Here's a possible solution that is UNTESTED. You'd need to add this at the top of the class
    Code:
    Imports System.Text.RegularExpressions
    Code:
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            If FolderBrowserDialog1.ShowDialog() = DialogResult.OK Then
                ' List files in the folder.
                ListFiles(FolderBrowserDialog1.SelectedPath)
                TextBox1.Text = (FolderBrowserDialog1.SelectedPath)
    
                For x As Integer = 0 To (Val(DataGridView1.Rows.Count) - 1)
                  
                    Dim filename As String = DataGridView1.Item(0, x).Value.ToString
                    Dim text As String = File.ReadAllText(FolderBrowserDialog1.SelectedPath + "\" + filename)
    
                    For Each match As Match In Regex.Matches(text, "COMBINED WITH", RegexOptions.IgnoreCase) 'change case option if that matters
                         DataGridView1.Rows.Add(New Object() {System.IO.Path.GetFileName(filename), text.Substring(match.Index, 60)})
                    Next
    
                Next
    
            End If
        End Sub
    However, it looks like you're putting filenames in the DGV and then adding rows to it with the matching strings. I don't believe that will mess up your For Loop count, but it just looks poor from a user interface standpoint. I'd put the strings into a second DGV. If you don't do that and it does evaluate Count each loop, then set a variable outside the For Loop and use that instead.

  6. #6

    Thread Starter
    New Member
    Join Date
    May 2017
    Posts
    12

    Re: Finding all keywords in a text file

    @jmcilhinney : For each text file it should be able to read the keyword "combine wih" together with the 60 character string. The output should be putting the filename in datagridview and adding row if there are morr keyword in a text file

  7. #7

    Thread Starter
    New Member
    Join Date
    May 2017
    Posts
    12

    Re: Finding all keywords in a text file

    @topshot ill gonna try this code tomorrow and ill give you a feedback thanks dude

  8. #8
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    98,679

    Re: Finding all keywords in a text file

    Quote Originally Posted by lockwater13 View Post
    For each text file it should be able to read the keyword "combine wih" together with the 60 character string.
    What 60 character string? You seem confused about what "a FULL and CLEAR description" is. I can't speak for others but if you're going to expect us to work out what you're trying to do from code that doesn't do it then you're not going to get much help from me, especially when you still refuse to provide a proper explanation even after being prompted twice.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width


×
We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.