Solved

Highlight Differenct character in 2 text files

Posted on 2006-11-13
8
175 Views
Last Modified: 2012-05-05
I have file A contain number from 1 2 3 4 5 6 7 8
I have file B contain number from 2 4 6 8
I need to develop program in vb to generate third file C containging only the differece Between A and B Like (1 3 5 7)

I did the following but now working ... anybody can help please?
Private Sub ReadandMatch_Click()
     
        'Read The Source File
        ff1 = FreeFile
        Open Text1.Text For Binary Access Read Lock Read Write As #ff1
        s1 = Space$(LOF(ff1))
        Get #ff1, , s1
       
        Arr1 = Split(s1, vbCrLf)
        Close ff1
         
        'Read The Profile File
        ff2 = FreeFile
        Open Text2.Text For Binary Access Read Lock Read Write As #ff2 ' if any error change it to #ff1
        s2 = Space$(LOF(ff2))
        Get #ff2, , s2
        Arr2 = Split(s2, vbCrLf)
        Close ff2
       
       
         ' Write to output file
        ff7 = FreeFile 'Text file format
        Open Text5.Text & ".txt" For Output As #ff7
        ff8 = FreeFile
        Open Text7.Text & ".txt" For Output As #ff8
           
        For O = 0 To UBound(Arr1)
            For P = 0 To UBound(Arr2)
                If Trim(Arr1(O)) = Trim(Arr2(P)) Then
                    Print #ff7, Trim(Arr1(O))
                    Exit For ' if match exit from the second loop
                ElseIf Trim(Arr1(O)) <> Trim(Arr2(P)) Then
                     Print #ff8, Trim(Arr1(O))  
                    Exit For
                End If
            Next
        Next
      Close ff7
      Close ff8
End Sub
0
Comment
Question by:haiderms
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
8 Comments
 
LVL 35

Accepted Solution

by:
Raynard7 earned 250 total points
ID: 17936517
Hi,
If you include the microsoft scripting runtime as a reference,
below should read in both of your files, then output any lines that are in one but are not in the other.

public sub readFiles()
dim dic1 as dictionary
dim dic2 as dictionary

set dic1 = getData("C:\temp\file1.txt")
set dic2 = getData("C:\temp\file1.txt")
outputDifferences(dic1, dic2)
end sub

public sub outputDifferences(dic1 as dictionary, dic2 as dictionary)
dim fileName as string
dim fileNo as integer
fileNAme = "C:\temp\diff.txt"
fileNo = freefile
dim x as variant
open filename for output as #fileno
for each x in dic1.keys
     if dic2.exists(x) = false then
        print #fileno, x
    end if
next x

for each x in dic2.keys
     if dic1.exists(x) = false then
        print #fileno, x
    end if
next x

close #fileno
end sub

public function getData(fileName as string) as dictionary
dim dicNew as new dictionary
dim fileNo as integer
dim strData as string
dim x as Variant

fileNo = freefile
open filename for binary access read lock read write as #fileNo
strData = Space$(LOF(fileNo))
Get #fileNo,,strData
   x = Split(strData, vbcrlf)
    For i = 0 To UBound(x)
        strData = x(i)
       if dicNew.exists(strData)
          dicNew(strData) = dicNew(strData) + 1
       else
          dicNew.add strData, 1
       end if
    Next i

getData = dicNew
end function
0
 

Author Comment

by:haiderms
ID: 17936594
sorry I think you miss understand me ... I don't it using VB6

My script working ok but result only
in file only 1,3

my problem I can't move from one array to the next array.
the problem on star mark line I need to know how can I do that ?
because if the condition match cursor should move to next line to compare with.

For O = 0 To UBound(Arr1)
            For P = 0 To UBound(Arr2)
                If Trim(Arr1(O)) = Trim(Arr2(P)) Then
                   
                    Print #ff7, Trim(Arr1(O))
                    Arr2(P) = Arr2(P) + 1   *********************************************(this is the problem)
                    Exit For ' if match exit from the second loop
                ElseIf Trim(Arr1(O)) <> Trim(Arr2(P)) Then

                     Print #ff8, Trim(Arr1(O))    
                    Exit For

                End If

            Next
           
        Next
0
 
LVL 35

Expert Comment

by:Raynard7
ID: 17936611
what are you doing it with?

also - what are you trying to acheive with Arr2(P) = Arr2(P) + 1  ??
0
 

Author Comment

by:haiderms
ID: 17936859

I am really sorry ... I mean I am doing this application using VB6.
Now This application should read to files and generate one file containg the difference lines not available on the second file.

Ar2 defined as long

what am tring to do is to move from one location in the array to anather
from example
First location in the array contain value of 2
Second location in the array contain value of 4
The third location in the array contain value of 6

my problem in the previous application is after matching the firs to lines in each file when find it math I should move to the second line on the second file to compare it
That why I add this statment
Arr2(P) = Arr2(P + 1)

so next time the for loop will take the value in the new location not same location.





0
 
LVL 35

Expert Comment

by:Raynard7
ID: 17937038
The above that I posted will work in vb6

if you must do it with arrays, I would do a foreach each way, ie go through the first file then for each record then loop through the other array - if the value is not found then print it out.

I would then reverse it the other way - for each record in file2 check each value in file1 - if not then print to your file.

you can see an example of how to look at each element in the array in my post
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question