This requires VS2008 or later:
vb.net Code:
Sub Process()
Dim ALines() As String = IO.File.ReadAllLines("A.txt")
Dim AHeader As String = Aggregate lines In ALines Where lines.StartsWith("ABC") Into First()
Dim ATrailer As String = Aggregate lines In ALines Where lines.StartsWith("T") Into First()
Dim Blines() As String = IO.File.ReadAllLines("B.txt")
Dim KeysB() As String = (From accounts In BLines Where accounts.StartsWith("D") Select accounts.Substring(1, 6)).ToArray
Dim Selected() As String = (From accounts In Alines Where KeysB.Contains(accounts.Substring(1, 6))).ToArray
Using tw As New IO.StreamWriter("C.txt")
tw.WriteLine(AHeader)
For Each line In Selected
tw.WriteLine(line)
Next
tw.WriteLine(ATrailer)
tw.WriteLine()
tw.Flush()
tw.Close()
End Using
End Sub