I have the following hash script that I use to compare two text files.
'Class
Public Class FileComparison
Public Class FileComparisonException
Public Enum ExceptionType
U 'Unknown
A 'Add
D 'Delete
M 'Modified
End Enum 'ExceptionType'
Public Line As String = String.Empty
Public Type As ExceptionType = ExceptionType.U
Public Sub New(ByVal newLine As String, ByVal newType As
ExceptionType)
Line = newLine
Type = newType
End Sub
End Class 'FileComparisonException'
Private m_listExceptions As New ArrayList
Public ReadOnly Property ExceptionList() As ArrayList
Get
Return m_listExceptions
End Get
End Property 'ExceptionList'
Private Function ParseFileText(ByVal text As String) As Hashtable
Dim lineText As String() = text.Split(vbCrLf)
Dim tableText As New Hashtable
For Each line As String In lineText
Dim colon As Integer = line.IndexOf(":")
If colon > 0 Then
Dim key As String = line.Substring(0, colon + 1).Trim()
tableText.Add(key, line)
End If
Next line
Return tableText
End Function 'ParseFileText'
Private Function ReadFile(ByVal path As String) As String
If IO.File.Exists(path) Then
Dim reader As New IO.StreamReader(path)
Dim fileText As String = reader.ReadToEnd()
reader.Close()
Return fileText
Else
Throw New IO.FileNotFoundException(path)
End If
End Function 'ReadFile'
Private Sub FindDeletionsModifications(ByVal htText1 As Hashtable,
ByVal htText2 As Hashtable)
For Each key1 As String In htText1.Keys
If Not htText2.ContainsKey(key1) Then
m_listExceptions.Add(New
FileComparisonException(htText1(key1),
FileComparisonException.ExceptionType.D))
Else
Dim lineText1 As String = htText1(key1)
Dim lineText2 As String = htText2(key1)
If Not lineText1.Equals(lineText2) Then
m_listExceptions.Add(New
FileComparisonException(htText2(key1),
FileComparisonException.ExceptionType.M))
End If
End If
Next key1
End Sub 'FindDeletionsModifications'
Private Sub FindAdditions(ByVal htText1 As Hashtable, ByVal htText2
As Hashtable)
For Each key2 As String In htText2.Keys
If Not htText1.ContainsKey(key2) Then
m_listExceptions.Add(New
FileComparisonException(htText2(key2),
FileComparisonException.ExceptionType.A))
End If
Next key2
End Sub 'FindAdditions'
Public Function Compare(ByVal file1 As String, ByVal file2 As
String)
Dim fileText1 As String = Me.ReadFile(file1)
Dim fileText2 As String = Me.ReadFile(file2)
Dim htText1 As Hashtable = Me.ParseFileText(fileText1)
Dim htText2 As Hashtable = Me.ParseFileText(fileText2)
Me.FindDeletionsModifications(htText1, htText2)
Me.FindAdditions(htText1, htText2)
End Function 'Compare'
End Class
'Module
Imports System.IO
Imports System.Threading
Module Module1
Sub Main()
Dim ThisGUID As Guid
Dim ThisGUIDString As String
ThisGUID = System.Guid.NewGuid()
ThisGUIDString = ThisGUID.ToString()
Dim library As New FileComparison
library.Compare("C:\VB Testing\Compare\MasterFile.txt", "C:\VB
Testing\Compare\CurrentFile.txt")
If library.ExceptionList.Count > 0 Then
For Each exception As
FileComparison.FileComparisonException In library.ExceptionList
Select Case exception.Type.ToString.Substring(0, 1)
Case "A"
AppendTextToFile(exception.Type.ToString & ":"
& exception.Line & ":" & ThisGUIDString)
Case "D", "M"
AppendTextToFile(exception.Type.ToString & ":"
& exception.Line)
End Select
Next
End If
End Sub
Sub AppendTextToFile(ByVal str As String)
Dim file As New System.IO.StreamWriter("C:\VB
Testing\Compare\Exception.txt", True)
file.WriteLine(str)
file.Close()
End Sub
End Module
Basically, I'm comparing two text files and the format of the
"Master.txt" file looks like this:
AB00001 :THIS JOB PRINTS THE MONTH-END CLOSING LEDGER & JOURNAL
REPORTS:REM0787:ALT0237:MAL3677:
046bd969-a642-42de-bdc4-624c9311096d
AB00003 :UPDATES BOOKKEEPING DATA BASES WITH BUSINESS TRANSACTIONS
:REM0787:ALT0237:MAL3677: 4b8971f4-79f2-451c-bcdd-778fc5cef690
AB00004 :THIS IS THE IMAGE COPY JOB FOR THE DB2 DATABASE DABKPNGP.
:COL0674:KER0464:WOC0323: 2451161b-647e-48d7-aa8c-c9d7ee0de078
AB00005 :CLOSES PREVIOUS FISCAL YEAR, PRODUCES DATA FILES FOR REPORTS
:REM0787:ALT0237:MAL3677: 423294b5-0a71-4480-acc1-42d56a51de77
AB00012 :CLOSES PREVIOUS FISCAL YEAR, PRODUCES DATA FILES FOR REPORTS
:REM0787:ALT0237:MAL3677: 423294b5-0a71-4480-acc1-42d56a51de77
AB00013 :CLOSES PREVIOUS FISCAL YEAR, PRODUCES DATA FILES FOR REPORTS
:REM0787:ALT0237:MAL3677: 423294b5-0a71-4480-acc1-42d56a51de77
AB00014 :CLOSES PREVIOUS FISCAL YEAR, PRODUCES DATA FILES FOR REPORTS
:REM0787:ALT0237:MAL3677: 423294b5-0a71-4480-acc1-42d56a51de77
The format of the "Current.txt" file looks like this:
AB00001 :THIS JOB PRINTS THE MONTH-END CLOSING LEDGER & JOURNAL
REPORTS:REM0787:ALT0237:MAL3677
AB00002 :THIS JOB PRINTS THE ANNUAL/FINAL CLOSINGLEDGER & JOURNAL
REPOR:REM0787:ALT0237:MAL3677
AB00003 :UPDATES BOOKKEEPING DATA BASES WITH BUSINESS TRANSACTIONS
:REM0787:ALT0237:MAL3677
AB00005 :CLOSES PREVIOUS FISCAL YEAR, PRODUCES DATA FILES FOR REPORTS
:REM0787:ALT0237:MAL3677
AB00012 :CLOSES PREVIOUS FISCAL YEAR, PRODUCES DATA FILES FOR REPORTS
:REM0787:KRE1973:MAL3677
AB00014 :CLOSES CURRENT YEAR, PRODUCES DATA FILES FOR REPORTS
:REM0787:ALT0237:MAL3677
The output exception file(Exception.txt) would look like this:
A:AB00002 :THIS JOB PRINTS THE ANNUAL/FINAL CLOSINGLEDGER & JOURNAL
REPOR:REM0787:ALT0237:MAL3677:2451761b-647e-48r7-bb8c-c9i7ee0de178
D:AB00004 ::COL0674:KER0464:WOC0323
D:AB00013 :::REM0787:ALT0237:MAL3677
M:AB00012 :::KRE1973:
M:AB00014 :CLOSES CURRENT YEAR, PRODUCES DATA FILES FOR REPORTS :::
Basically, the 'FindAdditions' code of finding new records is working,
because I write out the whole record...all the fields and I'm adding a
GUID 32-character alphanumeric number at the end of the record to the
'Exception.txt' file. The GUID key is needed because the file will be
imported into a LDAP server.
Description of the fields:
col. 0
A= Addtion
D=Deletion
M=Modified
Col. 1-8
JobName
col. 11-62
Job Description
col. 64-70
Group
col. 72-78
Primary
col. 80-86
Secondary
Now, the problem I'm having is how to display only the certain fields
in a record based on whether it's a "D" or "M" in the first column.
Examples of a "D" deletion record from above:
D:AB00004 ::COL0674:KER0464:WOC0323
D:AB00013 ::REM0787:ALT0237:MAL3677
In this case I DON'T want to display the 'Job Description' field, but I
want the rest of the fields. So, any records that have a "D" in column
one should only have the "D", JobName, Primary, Secondary, Tertairy
fields displayed.
Eample of a "M" modified record from above:
M:AB00012 :::KRE1973:
M:AB00014 :CLOSES CURRENT YEAR, PRODUCES DATA FILES FOR REPORTS :::
In the first record I just want to display the 'JobName' and the field
that's changed. You can see by the example that the 'Secondary' field
is different in the 'Current.txt' file from the 'Master.txt' file.
In the second record I just want to display the 'JobName' and the
field that's changed. You can see by this example that the
'JobDescription' field is different in the 'Current.txt' file from the
'Master.txt' file.
I hope I explained everything correctly. I don't think all the code has
to be re-written, just the 'FindDeletionsModifications" sub routine.
Thanks