Results 1 to 11 of 11

Thread: [RESOLVED] [2005] - Compare Text In 2 Files

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    111

    Resolved [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

  2. #2
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: [2005] - Compare Text In 2 Files

    would the names all be on seperate lines?
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    111

    Question 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.

  4. #4
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    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:
    1. Dim textone As String
    2.         Dim texttwo As String
    3.         Dim textonenames() As String
    4.  
    5.         textone = My.Computer.FileSystem.ReadAllText("C:\test1.txt")
    6.         texttwo = My.Computer.FileSystem.ReadAllText("C:\test2.txt")
    7.  
    8.         textonenames = textone.Split(vbNewLine)
    9.  
    10.         For Each line As String In textonenames
    11.             If texttwo.Contains(line) Then
    12.                 MsgBox(line)
    13.             End If
    14.  
    15.         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.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  5. #5
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    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?

  6. #6
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    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.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  7. #7

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    111

    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

  8. #8
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: [2005] - Compare Text In 2 Files

    So does the code I posted help you out at all?
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  9. #9

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    111

    Smile 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

  10. #10
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: [2005] - Compare Text In 2 Files

    Ah ok cool, let us know if it helps or not
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  11. #11

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    111

    Thumbs up 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
  •  



Click Here to Expand Forum to Full Width