vb.net - find dups and add

XK8ER
XK8ER used Ask the Experts™
on
Hello there,
I need to modify this little function so when I run it with these text files I would get the output.txt shown here.
how can I do that?
>db.txt
name1/3HGaDXwy/a
name2/iajgL9Aw/b/566/
name3/koyQ5dBp/c/688/956

>new.txt
name3
name4

>output.txt
name4

Open in new window

name3 was removed because its a dup of db.txt
Public Sub Rem_Dup(ByVal strFileDB As String, ByVal strFileNew As String, ByVal strFileOut As String)
        Dim TheDict As New Dictionary(Of String, String)
        For Each x In IO.File.ReadAllLines(strFileDB)
            If TheDict.ContainsKey(x.Split("/").First) Then Continue For
            TheDict.Add(x.Split("/").First, x)
        Next

        For Each x In IO.File.ReadAllLines(strFileNew)
            If TheDict.ContainsKey(x) Then TheDict.Remove(x)
        Next
        IO.File.WriteAllLines(strFileOut, TheDict.Values.ToList)
    End Sub

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Project manager
Commented:
Small change done. Only keep track of the ones that are not in the dictionary of the first file
Public Sub Rem_Dup(ByVal strFileDB As String, ByVal strFileNew As String, ByVal strFileOut As String)
        Dim x As String
        Dim TheDict As New Dictionary(Of String, String)
        Dim TheDictNew As New Dictionary(Of String, String)
        For Each x In IO.File.ReadAllLines(strFileDB)
            If TheDict.ContainsKey(x.Split("/").First) Then Continue For
            TheDict.Add(x.Split("/").First, x)
        Next

        For Each x In IO.File.ReadAllLines(strFileNew)
            'If TheDict.ContainsKey(x) Then TheDict.Remove(x)
            If String.IsNullOrWhiteSpace(x) = False And TheDict.ContainsKey(x) = False Then TheDictNew.Add(x, x)
        Next
        IO.File.WriteAllLines(strFileOut, TheDictNew.Values.ToList)
    End Sub

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial