473,416 Members | 1,719 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

Modify code using a hash table in comparing two text files

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

Nov 21 '05 #1
0 2369

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Chris McKeever | last post by:
I am trying to modify the Mailman Python code to stop mapping MIME-types and use the extension of the attachment instead. I am pretty much clueless as to what I need to do here, but I think I have...
7
by: Matthias Käppler | last post by:
Hi, I need to store objects of a FileInfo class containing information about a specific file. I can uniquely identify these files by their serial number. I thought it would be a good idea to use...
7
by: David. E. Goble | last post by:
Hi all; I have the following files; index.html, sigsheader.js, sigsboby.js, smilesbody.js and smiles.js. The sourse is below. The page displays two manual slide shows... Each slideshow has a set...
9
by: David. E. Goble | last post by:
Arrrh! some buttons work while others don't, but I can't see why. I have tried comparing the files that do work, with the ones that don't. But to no help. The funny thing is the parts that work...
2
by: bs | last post by:
I have a hashtable that contains structures. Is it possible to modify a value of one of these structures. It only seems to work if the hash contains class objects, not structures?
10
by: Qiangning Hong | last post by:
I'm writing a spider. I have millions of urls in a table (mysql) to check if a url has already been fetched. To check fast, I am considering to add a "hash" column in the table, make it a unique...
5
by: lavu | last post by:
I am trying to provide some security to text files, by adding a signature at the end of each text file. this signature needs to be generated by some kind of hashing algorithm. so while sending...
232
by: robert maas, see http://tinyurl.com/uh3t | last post by:
I'm working on examples of programming in several languages, all (except PHP) running under CGI so that I can show both the source files and the actually running of the examples online. The first...
23
by: raylopez99 | last post by:
A quick sanity check, and I think I am correct, but just to make sure: if you have a bunch of objects that are very much like one another you can uniquely track them simply by using an ArrayList...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.