By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,836 Members | 1,840 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,836 IT Pros & Developers. It's quick & easy.

Help needed, Removing duplicate lines in text file

P: n/a
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.Us erAppDataPath +
"\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

Jun 19 '06 #1
Share this Question
Share on Google+
10 Replies


P: n/a
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

Backwards wrote:
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.Us erAppDataPath +
"\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

Jun 19 '06 #2

P: n/a
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

--
Programming today is a race between software engineers striving to build
bigger and better idiot-proof programs, and the Universe trying to produce
bigger and better idiots. So far, the Universe is winning. (Rich Cook)

"R. MacDonald" <sc****@NO-SP-AMcips.ca> schreef in bericht
news:44***********************@news.wanadoo.nl...
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

Backwards wrote:
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.Us erAppDataPath +
"\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

Jun 19 '06 #3

P: n/a
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"

Jun 19 '06 #4

P: n/a
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/de...classtopic.asp
If it is about multi-user practise than you need of course a whatever
database

I hope this helps,

Cor

"Backwards" <An****@technicaltraining.co.nz> schreef in bericht
news:11**********************@f6g2000cwb.googlegro ups.com...
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.Us erAppDataPath +
"\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
Jun 19 '06 #5

P: n/a
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
Backwards wrote:
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"

Jun 19 '06 #6

P: n/a
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
Cor Ligthert [MVP] wrote:
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/de...classtopic.asp
If it is about multi-user practise than you need of course a whatever
database

I hope this helps,

Cor

"Backwards" <An****@technicaltraining.co.nz> schreef in bericht
news:11**********************@f6g2000cwb.googlegro ups.com...
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.Us erAppDataPath +
"\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

Jun 19 '06 #7

P: n/a
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

Backwards wrote:
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"

Jun 19 '06 #8

P: n/a
Randy, I see I wrote a R to much it had to be Andy.

:-)

Cor

"R. MacDonald" <sc****@NO-SP-AMcips.ca> schreef in bericht
news:44***********************@news.wanadoo.nl...
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
Cor Ligthert [MVP] wrote:
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/de...classtopic.asp
If it is about multi-user practise than you need of course a whatever
database

I hope this helps,

Cor

"Backwards" <An****@technicaltraining.co.nz> schreef in bericht
news:11**********************@f6g2000cwb.googlegro ups.com...
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.Us erAppDataPath +
"\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


Jun 19 '06 #9

P: n/a
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


R. MacDonald wrote:
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

Backwards wrote:
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"


Jun 19 '06 #10

P: n/a
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

Jun 19 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.