Help needed, Removing duplicate lines in text file

B

Backwards

Hello all,

I'll start by explaining what my app does so not to confuss you when i
ask my question. ☺

I have a VB.Net 2.0 app that starts a process (process.start ...) and
passes a prameter through from a combo box. The combo box items are
made up of IP address and computer host name. Anything a user places in
this combo box it writes this to a txt file called history.txt

My.Computer.FileSystem.WriteAllText(Application.UserAppDataPath +
"\History.txt", Me.ComboBox1.Text + vbCrLf, True)

When the application is restarted (closed and opend) its clears the
combo box items (me.combobox1.items.clear()) and then imports them from
the above history.txt file.

However the problem i have is with common address's that are used. (ie
192.168.0.1) these show in the list 4 or 5 times as they have been
typed into the combo box 4 -5 times by the user.

Is there anyway on form1.load to read this text file and remove any
line that has the same address?

If you need any more info please let me know.
All and any help would be nice.

Regards,

Andy
 
R

R. MacDonald

Hello, Andy,

There are different ways to approach this. The "best" way may depend on
how (and with what type of objects) you are now loading your combo-box.

In an earlier case I have done something similar by creating a class
that suppressed duplicate entries and used that to load the list.

But in your case it might be better simply to examine each entry in
History.txt before you place it into your ComboBox list to see if it
already exists.

If you can give some details (e.g. a code snippet) showing how you are
currently loading your list, you might get a better answer.

Cheers,
Randy


Perhaps you can best filter the
 
P

Peter Proost

Hi in addition to Randy,

If it's possible I would prefer to save the address history in a table then
it's very easy to do select distinct ....
But you can also do a select on a text file, for example:
Assuming this is your text file:
addresses.txt

ADDRESS
"192.168.1.1"
"192.168.1.1"
"192.168.1.3"
"192.168.1.5"
"192.168.1.5"

ADDRESS needs to be included, it's the columnheader, you can also do it
without a columnheader but my sample uses it.

Dim myCon As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=" & Application.StartupPath & "\" & ";Extended
Properties=""text;HDR=Yes;FMT=Delimited""")
Dim myAdapter As New OleDbDataAdapter("Select distinct address from
addresses.txt", myCon)
Dim myDataset As New DataSet

myCon.Open()
myAdapter.Fill(myDataset)
myCon.Close()

For i As Integer = 0 To myDataset.Tables(0).Rows.Count - 1
ComboBox1.Items.Add(CStr(myDataset.Tables(0).Rows(i).Item(0)))
Next


Hope this helps

Peter
 
B

Backwards

This is my code on form load that pulls the info from the history.txt

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Try
Me.ComboBox1.Items.Clear()

If Not File.Exists(Application.UserAppDataPath +
"\History.txt") Then
MessageBox.Show("History file was not found, no histroy
will be showen", Application.ProductName, MessageBoxButtons.OK,
MessageBoxIcon.Information)
'Return
End If
Using sr As StreamReader =
File.OpenText(Application.UserAppDataPath + "\History.txt")
Dim input As String
input = sr.ReadLine()
While Not input Is Nothing
'MessageBox.Show(input)
Me.ComboBox1.Items.Add(input)
input = sr.ReadLine()
End While
'MessageBox.Show("The end of the stream has been
reached.")
sr.Close()
End Using
Catch ex As Exception
MessageBox.Show(ex.Message, "Error"
 
C

Cor Ligthert [MVP]

Randy,

From what I understood (and I thought I did) in your message is the solution
for you to use the SortedList instead between the txt.file.

This can contains only one key.

http://msdn.microsoft.com/library/d...rlrfsystemcollectionssortedlistclasstopic.asp


If it is about multi-user practise than you need of course a whatever
database

I hope this helps,

Cor

"Backwards" <[email protected]> schreef in bericht
Hello all,

I'll start by explaining what my app does so not to confuss you when i
ask my question. ?

I have a VB.Net 2.0 app that starts a process (process.start ...) and
passes a prameter through from a combo box. The combo box items are
made up of IP address and computer host name. Anything a user places in
this combo box it writes this to a txt file called history.txt

My.Computer.FileSystem.WriteAllText(Application.UserAppDataPath +
"\History.txt", Me.ComboBox1.Text + vbCrLf, True)

When the application is restarted (closed and opend) its clears the
combo box items (me.combobox1.items.clear()) and then imports them from
the above history.txt file.

However the problem i have is with common address's that are used. (ie
192.168.0.1) these show in the list 4 or 5 times as they have been
typed into the combo box 4 -5 times by the user.

Is there anyway on form1.load to read this text file and remove any
line that has the same address?

If you need any more info please let me know.
All and any help would be nice.

Regards,

Andy
 
R

R. MacDonald

Hello, Andy,

Can you replace the ComboBox1.Items.Add line with:

If (Not ComboBox1.Items.Contains(input)) Then
ComboBox1.Items.Add(input)
End If

(Though, you may need to be a little more elaborate if you want your
comparison to be case insensitive.)

Cheers,
Randy
 
R

R. MacDonald

Hello, Cor,

Your guess is perfect. My class inherited SortedList. But I suspect
that this is more involved than what the OP requires.

(Oops, I seem to have posted my suggestion in the wrong sub-thread.)

Groetjes,
Randy
 
R

R. MacDonald

Hello, Andy,

Can you replace the ComboBox1.Items.Add line with:

If (Not ComboBox1.Items.Contains(input)) Then
ComboBox1.Items.Add(input)
End If

(Though, you may need to be a little more elaborate if you want your
comparison to be case insensitive.)

Cheers,
Randy
 
B

Backwards

Hello Randy,

This seems to have worked just how i wanted it to. Place 10 of the same
address's in the history.txt file but only one showed up in the combo
box.

Thanks to all for input on this and i hope it helps others in the
future.

Andy
 
B

Backwards

I was half asleep before and did the same thing said Randy when i
wanted to say thanks to R. MacDonald.

And of course to everyone else

regards,
Andy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top