-
Apr 15th, 2024, 09:27 AM
#1
Thread Starter
Addicted Member
[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
-
Apr 15th, 2024, 09:52 AM
#2
Re: Reading Textfile and comparing ListBox
 Originally Posted by Kochanski
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).
 Originally Posted by Kochanski
... 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.
-
Apr 15th, 2024, 10:54 AM
#3
Thread Starter
Addicted Member
Re: Reading Textfile and comparing ListBox
 Originally Posted by dday9
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.
-
Apr 15th, 2024, 11:07 AM
#4
Re: Reading Textfile and comparing ListBox
You need to either import the System.IO namespace or fully qualify the reference.
-
Apr 15th, 2024, 11:15 AM
#5
Thread Starter
Addicted Member
Re: Reading Textfile and comparing ListBox
 Originally Posted by dday9
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.
-
Apr 15th, 2024, 11:31 AM
#6
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
-
Apr 15th, 2024, 11:57 AM
#7
Thread Starter
Addicted Member
Re: Reading Textfile and comparing ListBox
 Originally Posted by dday9
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")
-
Apr 15th, 2024, 12:42 PM
#8
Re: [RESOLVED] Reading Textfile and comparing ListBox
In the future, so you know, that’s what I meant by “fully qualify the namespace”.
-
Apr 15th, 2024, 12:52 PM
#9
Thread Starter
Addicted Member
Re: [RESOLVED] Reading Textfile and comparing ListBox
 Originally Posted by dday9
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|