Home All Groups Group Topic Archive Search About

Help needed, Removing duplicate lines in text file

Author
19 Jun 2006 8:38 AM
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

Author
19 Jun 2006 9:31 AM
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

Backwards wrote:

Show quoteHide quote
> 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
>
Author
19 Jun 2006 9:40 AM
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



--
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)

Show quoteHide quote
"R. MacDonald" <sci***@NO-SP-AMcips.ca> schreef in bericht
news:44966e68$0$70752$dbd43001@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.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
> >
Author
19 Jun 2006 9:43 AM
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"
Author
19 Jun 2006 1:30 PM
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


Backwards wrote:
Show quoteHide quote
> 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"
>
Author
19 Jun 2006 1:34 PM
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

Backwards wrote:

Show quoteHide quote
> 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"
>
Author
19 Jun 2006 1:52 PM
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




R. MacDonald wrote:

Show quoteHide quote
> 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"
> >
Author
19 Jun 2006 2:02 PM
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
Author
19 Jun 2006 10:01 AM
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/default.asp?url=/library/en-us/cpref/html/frlrfsystemcollectionssortedlistclasstopic.asp


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

I hope this helps,

Cor

"Backwards" <And***@technicaltraining.co.nz> schreef in bericht
news:1150706307.028717.165760@f6g2000cwb.googlegroups.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.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
Author
19 Jun 2006 1:34 PM
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


Cor Ligthert [MVP] wrote:

Show quoteHide quote
> 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/default.asp?url=/library/en-us/cpref/html/frlrfsystemcollectionssortedlistclasstopic.asp
>
>
> If it is about multi-user practise than you need of course a whatever
> database
>
> I hope this helps,
>
> Cor
>
> "Backwards" <And***@technicaltraining.co.nz> schreef in bericht
> news:1150706307.028717.165760@f6g2000cwb.googlegroups.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.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
>
>
Author
19 Jun 2006 1:43 PM
Cor Ligthert [MVP]
Randy, I see I wrote a R to much it had to be Andy.

:-)

Cor

Show quoteHide quote
"R. MacDonald" <sci***@NO-SP-AMcips.ca> schreef in bericht
news:4496a74b$0$41882$dbd41001@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/default.asp?url=/library/en-us/cpref/html/frlrfsystemcollectionssortedlistclasstopic.asp
>>
>>
>> If it is about multi-user practise than you need of course a whatever
>> database
>>
>> I hope this helps,
>>
>> Cor
>>
>> "Backwards" <And***@technicaltraining.co.nz> schreef in bericht
>> news:1150706307.028717.165760@f6g2000cwb.googlegroups.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.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
>>