Results 1 to 2 of 2

Thread: compare 2 files

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2008
    Posts
    182

    compare 2 files

    i need an app which can compare 2 files.

    say i have tewo text files; file 1 and file 2

    file 1 is the main file and file 2 is a file which i add lines into. so if i load file 1 in text box 1 and then load file 2 in text box 2 and then i would like to click compare and would like it to not do anything to the data in file 1 nor the text box 1 but to compare the lines in text box 2 with text box 1 and remove any duplicates.


    file 1
    a 1
    ab 2
    abc abc
    abcd 12345
    abcde
    abcdef


    file 2

    ab 2
    asd
    wer
    abcd 12345
    abc abc
    ab 2

    after clicking remove duplicated i would like it to that the results from text box 2 if duplicates are removed from text box 2 not 1

    text box 2 final
    asd
    wer


    can any one help?

  2. #2
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: compare 2 files

    You can split text in both textboxes into 2 arrays then compare them to remove duplicate lines.

    This is another way:
    Code:
    Private Sub Command1_Click()
        Dim sText1 As String, sText2 As String
        Dim sItems() As String, i As Long
        
        '-- copy and add first and last delimiters
        sText1 = vbLf & Text1.Text & vbCr
        '-- split Text2.Text by vbCrLf
        sItems = Split(Text2.Text, vbCrLf)
        '-- check existing of each item
        sText2 = ""
        For i = 0 To UBound(sItems)
            '-- surround sItems(i) with vbLf and vbCr then check
            If InStr(sText1, vbLf & sItems(i) & vbCr) = 0 Then
                '-- sItem2(i) does not exist in sText1, add it to sText2
                sText2 = sText2 & vbCrLf & sItems(i)
            End If
        Next
        '-- remove leading vbCrLf and copy to Text2.Text
        Text2.Text = Mid$(sText2, 3)
    End Sub
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

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