Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

compare two lists 1

Status
Not open for further replies.

Xsi

Programmer
May 29, 2015
121
SE
Hello people,

I have got a program that compares two text files.

Code:
 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
 

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!
 
Hi jebenson,

I will test it very soon thank you!
 
Here's a shorter LINQ-based variant:
Code:
[blue]        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))[/blue]
 
thank you very much @strongm!!
work as a charm!
 
a question How do I add create a list / string of
Code:
Console.WriteLine(String.Join(Environment.NewLine, differenceQuery))
when I try it says doesn't produce a value... either everything is on a line.
 
Sorry, I assumed you core problem was the list comparison, rather than creating a new list.

This:

[tt]String.Join(Environment.NewLine, differenceQuery)[/tt]

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

So if you want that new string:

[tt]Dim myNewString as string = String.Join(Environment.NewLine, differenceQuery)[/tt]

Alternatively if you want it as a string list:

[tt]Dim myNewStringList As List(Of String) = differenceQuery.ToList[/tt]

 
when I use
Code:
 Dim myNewString as string = String.Join(Environment.NewLine, differenceQuery)]
everything is one line.
 
I solved it by use
Code:
 For Each item In differenceQuery
            CleanStringList.Add(item.ToString)
        Next
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top