Results 1 to 9 of 9

Thread: [RESOLVED] Reading Textfile and comparing ListBox

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2009
    Posts
    243

    Resolved [RESOLVED] Reading Textfile and comparing ListBox

    This got complicated with read/write and looping.
    I have a list of names in a ListBox and I want to loop through them to see if any of them are in a longer textfile called Group1.txt. If they are, (Instr) line by line, I want to write the complete line from Group1.txt, which includes an email address to a new textfile called Group2.txt In the example below, I thought I would write to a textbox, but that does not help me compare anything or write lines to to Group2.txt. The Listbox contains Forename and Surname e.g. Fred Bloggs but Group1.txt contains Fred Bloggs [email protected], for example. I need to get that full line into a new Group2.txt file if it is in the ListBox.

    HTML Code:
     Dim sItems As String = ""
            For intIdx As Int32 = 0 To Me.ListBox3.Items.Count - 1
                sItems += (Me.ListBox3.Items.Item(intIdx) & vbCrLf)
            Next
    
            TextBox1.Text = sItems

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    12,089

    Re: Reading Textfile and comparing ListBox

    Quote Originally Posted by Kochanski View Post
    I have a list of names in a ListBox and I want to loop through them to see if any of them are in a longer textfile called Group1.txt. If they are, (Instr) line by line, ...
    Use IO.File.ReadLines (documentation) and for the love of God, do not use InStr. Use String.Contains (documentation) or String.IndexOf (documentation).

    Quote Originally Posted by Kochanski View Post
    ... I want to write the complete line from Group1.txt ...
    Use IO.File.AppendAllLines (documentation).

    Here is an example:
    Code:
    Dim fileToRead = "Group1.txt" ' change me
    Dim fileToWrite = "Group2.txt" ' change me
    For Each listboxItem In ListBox3.Items
        Dim listboxValue = listboxItem.ToString() ' assuming it is a string
        Dim match = File.ReadLines(fileToRead).FirstOrDefault(Function(line) line.Contains(listboxValue))
    
        File.AppendAllLines(fileToWrite, { match })
    Next
    Edit - Thinking about it a little more I would probably wait to write to the file after the looping is done. Instead add the matches to a List(Of String) and write it all at once:
    Code:
    Dim fileToRead = "Group1.txt" ' change me
    Dim fileToWrite = "Group2.txt" ' change me
    Dim matches = New List(Of String)
    For Each listboxItem In ListBox3.Items
        Dim listboxValue = listboxItem.ToString() ' assuming it is a string
        Dim match = File.ReadLines(fileToRead).FirstOrDefault(Function(line) line.Contains(listboxValue))
    
        matches.Add(match)
    Next
    File.AppendAllLines(fileToWrite, matches)
    There is also no error handling in this example, I'm leaving that up to you what you should do if there is no match in the for/each.
    Last edited by dday9; Apr 15th, 2024 at 10:11 AM.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jan 2009
    Posts
    243

    Re: Reading Textfile and comparing ListBox

    Quote Originally Posted by dday9 View Post
    Use IO.File.ReadLines (documentation) and for the love of God, do not use InStr. Use String.Contains (documentation) or String.IndexOf (documentation).


    Use IO.File.AppendAllLines (documentation).

    Here is an example:
    Code:
    Dim fileToRead = "Group1.txt" ' change me
    Dim fileToWrite = "Group2.txt" ' change me
    For Each listboxItem In ListBox3.Items
        Dim listboxValue = listboxItem.ToString() ' assuming it is a string
        Dim match = File.ReadLines(fileToRead).FirstOrDefault(Function(line) line.Contains(listboxValue))
    
        File.AppendAllLines(fileToWrite, { match })
    Next
    Edit - Thinking about it a little more I would probably wait to write to the file after the looping is done. Instead add the matches to a List(Of String) and write it all at once:
    Code:
    Dim fileToRead = "Group1.txt" ' change me
    Dim fileToWrite = "Group2.txt" ' change me
    Dim matches = New List(Of String)
    For Each listboxItem In ListBox3.Items
        Dim listboxValue = listboxItem.ToString() ' assuming it is a string
        Dim match = File.ReadLines(fileToRead).FirstOrDefault(Function(line) line.Contains(listboxValue))
    
        matches.Add(match)
    Next
    File.AppendAllLines(fileToWrite, matches)
    There is also no error handling in this example, I'm leaving that up to you what you should do if there is no match in the for/each.
    Thanks for this. The only error signaled is it says that the word File is not declared. It s happy with the rest of the code.

  4. #4
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    12,089

    Re: Reading Textfile and comparing ListBox

    You need to either import the System.IO namespace or fully qualify the reference.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jan 2009
    Posts
    243

    Re: Reading Textfile and comparing ListBox

    Quote Originally Posted by dday9 View Post
    You need to either import the System.IO namespace or fully qualify the reference.
    I am sure you are right, but I have no idea how to do that as my skills are a bit limited. I had hoped it would be happy with yoyur code as posted. Any further help is appreciated.

  6. #6
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    12,089

    Re: Reading Textfile and comparing ListBox

    Importing namespaces is sort of an important (and easy) thing to know how to do.

    Take a look at the official documentation here: https://learn.microsoft.com/en-us/do...space-and-type

    Here is a link to my own tutorial that doesn't go over importing the System.IO namespace explicitly but demonstrates how it would be done: https://www.vblessons.com/lessons.html#/3/2
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Jan 2009
    Posts
    243

    Re: Reading Textfile and comparing ListBox

    Quote Originally Posted by dday9 View Post
    Importing namespaces is sort of an important (and easy) thing to know how to do.

    Take a look at the official documentation here: https://learn.microsoft.com/en-us/do...space-and-type

    Here is a link to my own tutorial that doesn't go over importing the System.IO namespace explicitly but demonstrates how it would be done: https://www.vblessons.com/lessons.html#/3/2
    Resolved. I just changed File to IO.File and it all worked.
    HTML Code:
    Dim fileToRead = myFolderName & "Group1.txt" ' change me
            Dim fileToWrite = myFolderName & "Group3.txt" ' change me
            Dim matches = New List(Of String)
            For Each listboxItem In ListBox3.Items
                Dim listboxValue = listboxItem.ToString() ' assuming it is a string
                Dim match = IO.File.ReadLines(fileToRead).FirstOrDefault(Function(line) line.Contains(listboxValue))
    
                matches.Add(match)
            Next
            IO.File.AppendAllLines(fileToWrite, matches)
            Process.Start(myFolderName & "Group3.txt")

  8. #8
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    12,089

    Re: [RESOLVED] Reading Textfile and comparing ListBox

    In the future, so you know, that’s what I meant by “fully qualify the namespace”.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Jan 2009
    Posts
    243

    Re: [RESOLVED] Reading Textfile and comparing ListBox

    Quote Originally Posted by dday9 View Post
    In the future, so you know, that’s what I meant by “fully qualify the namespace”.
    Thank you for this. A quite tricky bit of read/write made simple by your code.

Posting Permissions

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



Click Here to Expand Forum to Full Width