|
-
May 1st, 2008, 03:12 AM
#1
Thread Starter
Lively Member
[RESOLVED] [2005] - Compare Text In 2 Files
Hi All,
I been asked to write a program that will compare the contents of 2 files.
Example of the 2 files.
Text File 1:
Bloggs, Joe, Sydney
Text File 2:
Bloggs, Joe, Sydney
Smith, Stan, Perth
The program should find "Bloggs, Joe, Sydney" as a match.
I was thinking that the files could be read into seperate arrays but there could be 5000+ lines from each file. Any idea's
-
May 1st, 2008, 03:20 AM
#2
Re: [2005] - Compare Text In 2 Files
would the names all be on seperate lines?
-
May 1st, 2008, 03:52 AM
#3
Thread Starter
Lively Member
Re: [2005] - Compare Text In 2 Files
Hi,
Yes, each name would be on a separate line. The possibility is that the 2 files would be in .CSV format which each line containing the person's name, address, telephone.
For now, I would be limiting the value to the person's first name, surname and suburb. Each value would be separated by a comma.
-
May 1st, 2008, 04:21 AM
#4
Re: [2005] - Compare Text In 2 Files
OK here's one way you could do it, its probably not the best way but its all I could come up with.
vb Code:
Dim textone As String
Dim texttwo As String
Dim textonenames() As String
textone = My.Computer.FileSystem.ReadAllText("C:\test1.txt")
texttwo = My.Computer.FileSystem.ReadAllText("C:\test2.txt")
textonenames = textone.Split(vbNewLine)
For Each line As String In textonenames
If texttwo.Contains(line) Then
MsgBox(line)
End If
Next
The only problem with this method is that the file you are comparing to (C:\test2.txt in my example) MUST have a blank line at the start of it for this to work... which is a bit pants really so im sure there will be a better solution.
-
May 1st, 2008, 07:29 AM
#5
Re: [2005] - Compare Text In 2 Files
How do you define a match? For example, in file1 you have:
Code:
Joe, Jane, Nick
Brian, Helen, Jim
Tom, Bill, Paul
And in file2 you have
Code:
Nick, Joe, Jane
Tom, Helen, Jim
Paul, Brian, Bill
Note that both files contain the same names, only in different listing order.
In this case, do you have a match or not?
-
May 1st, 2008, 07:38 AM
#6
Re: [2005] - Compare Text In 2 Files
I'm guessing he needs the lines to match exactly as he says this may be in csv format, which often means each string between the comma's represents specific data that is the same for each line. E.g first word = first name, next word after comma = surname, next word after comma = town etc etc
Im sure you probably already knew that though 
In the examples he gave, the first string was a surname, the second string was a first name and the last string in each line was the name of a place so it does look like each line represents just one person
So Joe, Bloggs, Sydney isnt the same as Bloggs, Joe, Sydney but of course it would be helpful for the thread starter to confirm
Last edited by chris128; May 1st, 2008 at 07:42 AM.
-
May 1st, 2008, 08:14 AM
#7
Thread Starter
Lively Member
Re: [2005] - Compare Text In 2 Files
Hi chris128,
You are correct that it goes; Surname, First Name, Location. Each line will represent 1 person
If File 1 had "Bloggs, Joe, Sydney" and File 2 had "Joe, Bloggs, Sydney", this would be considered not a match
-
May 1st, 2008, 08:41 AM
#8
Re: [2005] - Compare Text In 2 Files
So does the code I posted help you out at all?
-
May 1st, 2008, 06:37 PM
#9
Thread Starter
Lively Member
Re: [2005] - Compare Text In 2 Files
Hi chris128,
Haven't been into the office yet but when I have implemented the posted code, I will let you know
-
May 1st, 2008, 06:41 PM
#10
Re: [2005] - Compare Text In 2 Files
Ah ok cool, let us know if it helps or not
-
May 1st, 2008, 10:21 PM
#11
Thread Starter
Lively Member
Re: [2005] - Compare Text In 2 Files
Hi chris128,
You posted code works perfectly after placing a blank line at the start of the second file.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|