Bobby_Boner Bobby_Boner -4 years ago 74
Vb.net Question

Item pairing between two .txt

I have been trying to combine or pair two text files.
One file contains User:Key
The other file contains Key:Pass
I want a 3rd text file created containing the corresponding pairs of User:Pass based on the key matching.
Here is what Ive tried most recently

Private Sub Rotate()
Dim Cracked() As String = IO.File.ReadAllLines(TextBox1.Text)
For Each lineA In Cracked
TextBox5.Text = lineA
check()
Next
End Sub
Private Sub check()
Dim toCheck() As String = TextBox5.Text.Split(":")
Dim tHash As String = toCheck(0)
Dim tPass As String = toCheck(1)
Dim lines1() As String = IO.File.ReadAllLines(TextBox2.Text)
For Each line In lines1
If lines1.Contains(tHash) Then
Dim toAdd() As String = line.Split(":")
Dim uHash As String = toCheck(0)
Dim uUser As String = toCheck(1)
ListBox1.Items.Add(uUser + ":" + tPass)
End If
Next
End Sub
Public Sub CopyListBoxToClipboard(ByVal ListBox2 As ListBox)

Dim buffer As New StringBuilder

For i As Integer = 0 To ListBox1.Items.Count - 1
buffer.Append(ListBox1.Items(i).ToString)
buffer.Append(vbCrLf)
Next

My.Computer.Clipboard.SetText(buffer.ToString)

End Sub


Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
CopyListBoxToClipboard(ListBox1)
End Sub


The delimiter changes but for now the : works.
I tried splitting and matching but either the textbox5 does not rotate or it rotates through the list and thats all.

Answer Source

Something like this?

Dim KeyPassFile As String = "..."
Dim UserKeyFile As String = "..."
Dim UserPassFile As String = "..."

Dim KeyPass As New Hashtable

' Read Key:Pass file 
For Each Line In IO.File.ReadAllLines(KeyPassFile)
    Dim iStart = Line.IndexOf(":")
    Dim Key = Line.Substring(0, iStart)
    Dim Pass = Line.Substring(iStart + 1)
    KeyPass.Add(Key, Pass)
Next

' Create User:Pass file
Dim OutFile = IO.File.CreateText(UserPassFile)

' Read User:Key file
For Each Line In IO.File.ReadAllLines(UserKeyFile)
    Dim iStart = Line.IndexOf(":")
    Dim User = Line.Substring(0, iStart)
    Dim Key = Line.Substring(iStart + 1)
    If KeyPass.ContainsKey(Key) Then
        ' We have a match for the key, write it to the file
        OutFile.WriteLine(User & ":" & KeyPass(Key))
    End If
Next

OutFile.Close()

This will probably not work for very large files that doesn't fit in memory, and there is no duplicate check for the key insertion in the hashtable, but I'll leave something for you to do.. :)

Also, in your code, you read the file specified in the TextBox2.Text as many times as there are lines in the TextBox1.Text file..

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download