INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

compare two lists

compare two lists

(OP)
Hello people,

I have got a program that compares two text files.

CODE --> VB

Dim fileA = File.ReadAllLines("C:\Paths.txt")
        Dim fileB = File.ReadAllLines("C:\OrderNames.txt")
        Dim noAdress = String.Join(Environment.NewLine, fileB.Where(Function(b) Not fileA.Contains(b)))
        Console.WriteLine(noAdress) 

C:\Paths.txt contains:
X:\archive\100-101\234234\123
X:\archive\100-101\234234\124
X:\archive\100-101\26564\125
X:\archive\100-101\254634\126
X:\archive\100-101\234534\127
X:\archive\100-101\2323434\128

and C:\OrderNames.txt contains:
123
124
126
128


I want now remove the entries from Paths.txt that doesn't exists in OrderNames.txt / add them to a list in vb that is ok too.

Result:
C:\Paths.txt contains:
X:\archive\100-101\234234\123
X:\archive\100-101\234234\124
X:\archive\100-101\254634\126
X:\archive\100-101\2323434\128


Could someone help me?

Thank you in advance

RE: compare two lists


OK, this is a bit convoluted and there may be a better way, but here's how I'd do this.

CODE

Dim srPath As StreamReader

        srPath = New StreamReader(Application.StartupPath & "\Paths.txt")

        Dim srOrderNames As StreamReader

        srOrderNames = New StreamReader(Application.StartupPath & "\OrderNames.txt")

        Dim dt As DataTable

        Dim dc As DataColumn

        dc = New DataColumn("Path")

        dt = New DataTable

        dt.Columns.Add(dc)

        dc = New DataColumn("Last3")

        dt.Columns.Add(dc)

        Dim dr As DataRow

        Dim s As String

        Do While srPath.Peek <> -1
            s = srPath.ReadLine

            dr = dt.NewRow

            dr.Item("Path") = s

            dr.Item("Last3") = s.Substring(s.Length - 3)

            dt.Rows.Add(dr)

        Loop

        srPath.Close()

        dt.AcceptChanges()

        s = ""

        s = "Last3 NOT IN ("

        Do While srOrderNames.Peek <> -1
            s &= "'" & srOrderNames.ReadLine & "',"
        Loop

        s = s.Substring(0, s.Length - 1)
        s &= ")"

        dt.DefaultView.RowFilter = s

        For Each drv As DataRowView In dt.DefaultView
            ListBox1.Items.Add(drv.Item("Path"))
            drv.Delete()
        Next
        
        dt.DefaultView.RowFilter = ""
        dt.AcceptChanges()

        srOrderNames.Close() 

Basically, read Paths.txt into a datatable with 2 fields: one for the full path and the other for just the last 3 characters. Use OrderNames.txt to build a RowFilter string for the datatable, filter by that string and what you have is only the ones that do not match OrderNames.txt. Loop through those, add them to the listbox and delete them from the datatable.

After this you would just loop through the records remaining in the datatable (dt), and write Path back to a text file, which you would then save over the original Paths.txt file. Let me know if you need code for that, or any further explanation of the code above.

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!

RE: compare two lists

(OP)
Hi jebenson,

I will test it very soon thank you!

RE: compare two lists

Here's a shorter LINQ-based variant:

CODE

        Dim fileA = File.ReadAllLines("C:\Paths.txt")
        Dim fileB = File.ReadAllLines("C:\OrderNames.txt")
        Dim differenceQuery = From line In fileA
                            Let w = line.Split({"\"}, StringSplitOptions.RemoveEmptyEntries)
                            Where w.Distinct().Intersect(fileB).Count = 1
                            Select line
        Console.WriteLine(String.Join(Environment.NewLine, differenceQuery)) 

RE: compare two lists

(OP)
thank you very much @strongm!!
work as a charm!

RE: compare two lists

(OP)
a question How do I add create a list / string of

CODE --> VB.net

Console.WriteLine(String.Join(Environment.NewLine, differenceQuery)) 
when I try it says doesn't produce a value... either everything is on a line.

RE: compare two lists

Sorry, I assumed you core problem was the list comparison, rather than creating a new list.

This:

String.Join(Environment.NewLine, differenceQuery)

creates a string (which Console.WriteLine outputs if your app happens to be a console application)

So if you want that new string:

Dim myNewString as string = String.Join(Environment.NewLine, differenceQuery)

Alternatively if you want it as a string list:

Dim myNewStringList As List(Of String) = differenceQuery.ToList

RE: compare two lists

(OP)
when I use

CODE

Dim myNewString as string = String.Join(Environment.NewLine, differenceQuery)] 
everything is one line.

RE: compare two lists

(OP)
I solved it by use

CODE --> VB.net

For Each item In differenceQuery
            CleanStringList.Add(item.ToString)
        Next 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close