473,837 Members | 1,426 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 FileComparisonE xception

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 'FileComparison Exception'

Private m_listException s As New ArrayList

Public ReadOnly Property ExceptionList() As ArrayList
Get
Return m_listException s
End Get
End Property 'ExceptionList'

Private Function ParseFileText(B yVal text As String) As Hashtable
Dim lineText As String() = text.Split(vbCr Lf)
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(k ey, 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.ReadToEn d()
reader.Close()
Return fileText
Else
Throw New IO.FileNotFound Exception(path)
End If
End Function 'ReadFile'

Private Sub FindDeletionsMo difications(ByV al htText1 As Hashtable,
ByVal htText2 As Hashtable)
For Each key1 As String In htText1.Keys
If Not htText2.Contain sKey(key1) Then
m_listException s.Add(New
FileComparisonE xception(htText 1(key1),
FileComparisonE xception.Except ionType.D))
Else
Dim lineText1 As String = htText1(key1)
Dim lineText2 As String = htText2(key1)
If Not lineText1.Equal s(lineText2) Then
m_listException s.Add(New
FileComparisonE xception(htText 2(key1),
FileComparisonE xception.Except ionType.M))
End If
End If
Next key1
End Sub 'FindDeletionsM odifications'

Private Sub FindAdditions(B yVal htText1 As Hashtable, ByVal htText2
As Hashtable)
For Each key2 As String In htText2.Keys
If Not htText1.Contain sKey(key2) Then
m_listException s.Add(New
FileComparisonE xception(htText 2(key2),
FileComparisonE xception.Except ionType.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(fil e1)
Dim fileText2 As String = Me.ReadFile(fil e2)

Dim htText1 As Hashtable = Me.ParseFileTex t(fileText1)
Dim htText2 As Hashtable = Me.ParseFileTex t(fileText2)

Me.FindDeletion sModifications( htText1, htText2)
Me.FindAddition s(htText1, htText2)
End Function 'Compare'
End Class

'Module
Imports System.IO
Imports System.Threadin g

Module Module1
Sub Main()
Dim ThisGUID As Guid
Dim ThisGUIDString As String
ThisGUID = System.Guid.New Guid()
ThisGUIDString = ThisGUID.ToStri ng()

Dim library As New FileComparison

library.Compare ("C:\VB Testing\Compare \MasterFile.txt ", "C:\VB
Testing\Compare \CurrentFile.tx t")

If library.Excepti onList.Count > 0 Then
For Each exception As
FileComparison. FileComparisonE xception In library.Excepti onList
Select Case exception.Type. ToString.Substr ing(0, 1)
Case "A"
AppendTextToFil e(exception.Typ e.ToString & ":"
& exception.Line & ":" & ThisGUIDString)
Case "D", "M"
AppendTextToFil e(exception.Typ e.ToString & ":"
& exception.Line)
End Select
Next
End If
End Sub

Sub AppendTextToFil e(ByVal str As String)
Dim file As New System.IO.Strea mWriter("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:MAL367 7:
046bd969-a642-42de-bdc4-624c9311096d
AB00003 :UPDATES BOOKKEEPING DATA BASES WITH BUSINESS TRANSACTIONS
:REM0787:ALT023 7:MAL3677: 4b8971f4-79f2-451c-bcdd-778fc5cef690
AB00004 :THIS IS THE IMAGE COPY JOB FOR THE DB2 DATABASE DABKPNGP.
:COL0674:KER046 4:WOC0323: 2451161b-647e-48d7-aa8c-c9d7ee0de078
AB00005 :CLOSES PREVIOUS FISCAL YEAR, PRODUCES DATA FILES FOR REPORTS
:REM0787:ALT023 7:MAL3677: 423294b5-0a71-4480-acc1-42d56a51de77
AB00012 :CLOSES PREVIOUS FISCAL YEAR, PRODUCES DATA FILES FOR REPORTS
:REM0787:ALT023 7:MAL3677: 423294b5-0a71-4480-acc1-42d56a51de77
AB00013 :CLOSES PREVIOUS FISCAL YEAR, PRODUCES DATA FILES FOR REPORTS
:REM0787:ALT023 7:MAL3677: 423294b5-0a71-4480-acc1-42d56a51de77
AB00014 :CLOSES PREVIOUS FISCAL YEAR, PRODUCES DATA FILES FOR REPORTS
:REM0787:ALT023 7:MAL3677: 423294b5-0a71-4480-acc1-42d56a51de77
The format of the "Current.tx t" file looks like this:

AB00001 :THIS JOB PRINTS THE MONTH-END CLOSING LEDGER & JOURNAL
REPORTS:REM0787 :ALT0237:MAL367 7
AB00002 :THIS JOB PRINTS THE ANNUAL/FINAL CLOSINGLEDGER & JOURNAL
REPOR:REM0787:A LT0237:MAL3677
AB00003 :UPDATES BOOKKEEPING DATA BASES WITH BUSINESS TRANSACTIONS
:REM0787:ALT023 7:MAL3677
AB00005 :CLOSES PREVIOUS FISCAL YEAR, PRODUCES DATA FILES FOR REPORTS
:REM0787:ALT023 7:MAL3677
AB00012 :CLOSES PREVIOUS FISCAL YEAR, PRODUCES DATA FILES FOR REPORTS
:REM0787:KRE197 3:MAL3677
AB00014 :CLOSES CURRENT YEAR, PRODUCES DATA FILES FOR REPORTS
:REM0787:ALT023 7:MAL3677


The output exception file(Exception. txt) would look like this:

A:AB00002 :THIS JOB PRINTS THE ANNUAL/FINAL CLOSINGLEDGER & JOURNAL
REPOR:REM0787:A LT0237:MAL3677: 2451761b-647e-48r7-bb8c-c9i7ee0de178
D:AB00004 ::COL0674:KER04 64:WOC0323
D:AB00013 :::REM0787:ALT0 237: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:KER04 64:WOC0323
D:AB00013 ::REM0787:ALT02 37: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 'FindDeletionsM odifications" sub routine.

Thanks

Nov 21 '05 #1
0 2395

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

Similar topics

0
2418
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 narrowed it down to the Scrubber.py file.. If this seems like a quick step me through, I would be very appreciative, could get you something on your Amazon wish-list (that is me on my knees begging).. From just my basic understanding, it...
7
22334
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 a hash map so I can access the file information in constant time, without having to iterate over a (possibly very large) list of files. As far as I know, std::map does not hash its entries (I guess it takes O(nlogn) time to find an entry)....
7
1644
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 of buttons. the top slideshow (the smiles work fine. How ever the sigs slideshow displays the pictures in the smiles section. *************** index.html ***************************** <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01...
9
1842
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 have the same code, except different variables. (Note Javascript and htm code was writen by a c program, the code then run thru a html to js converter)
2
5027
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
2461
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 key, and use the following sql statement: insert ignore into urls (url, hash) values (newurl, hash_of_newurl) to add new url. I believe this will be faster than making the "url" column unique key and doing string comparation. Right?
5
2085
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 the file, i will sign the text file and at the receiving end, the text file will be checked to make sure that the hash code matches. ( not that it matters for this but my environment is c++ in vs.net2003) any ideas about what kind of hash...
232
13407
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 set of examples, after decoding the HTML FORM contents, merely verifies the text within a field to make sure it is a valid representation of an integer, without any junk thrown in, i.e. it must satisfy the regular expression: ^ *?+ *$ If the...
23
5749
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 or Array, correct? An example: create the object, create an array, the stuff the object into the array. Later on, assume the object is mutable, the object changes, but you can find it, if you have enough state information to uniquely identify...
0
9843
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10882
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10577
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10630
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7811
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5670
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5851
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4476
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4045
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.