Results 1 to 22 of 22

Thread: Problem again (regarding loop): how to compare certain part between files

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2007
    Posts
    92

    Problem again (regarding loop): how to compare certain part between files

    i got 2 INI file (file1 and file2), i wish to compare the specific part of the file, below the compare part only true if file 1 and file2 have an identical line data. (mean line7(aaa=...) of file1 must same as line7(aaa=...) of file2)

    if line7(aaa=...) but file2's line 7 = [DEF] ... it will show the content of aaa is different.



    File1:
    [ABC]
    aaa=1
    aab=2
    aac=3

    [DEF]
    aaa=13
    aab=24
    aac=3

    [GHI]
    aaa=1
    aab=24
    aac=35

    File2:
    [ABC]
    aaa=17
    aab=2
    aac=3

    [DEF]
    aaa=13
    aab=33
    aac=35

    [GHI]
    aaa=1
    aab=24
    aac=35
    Last edited by yamaha102; Jul 1st, 2007 at 07:57 AM.

  2. #2

    Thread Starter
    Lively Member
    Join Date
    Jun 2007
    Posts
    92

    Re: (help)compare certain part in file

    anyone please...

  3. #3
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,628

    Re: (help)compare certain part in file

    not sure what you mean make code is robust.
    Your question isn't exactly clear. Please give more detail in how you would like the output to be. I am not sure but are you only wanting to compare the file inside the header you specify?
    My light show youtube page (it's made the news) www.youtube.com/@lightsofelberfeld
    Contact me on the socials www.facebook.com/lordorwell

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Jun 2007
    Posts
    92

    Re: (help)compare certain part in file

    Quote Originally Posted by Lord Orwell
    not sure what you mean make code is robust.
    Your question isn't exactly clear. Please give more detail in how you would like the output to be. I am not sure but are you only wanting to compare the file inside the header you specify?

    sorry about my poor problem description.
    Actually what i want is the code to compare two part inside two INI files. let say [DEF] part as below INI files.(the comparison can list out that eee is different from 1st file and 2nd file.)



    File1:
    [ABC]
    aaa=1
    aab=2
    aac=3

    [DEF]
    aaa=13
    eee=24
    aac=3

    [GHI]
    aaa=1
    aab=24
    aac=35

    File2:

    [GHI]
    aaa=1
    aab=24
    aac=35

    [ABC]
    aaa=17
    aab=2
    aac=3

    [DEF]
    aaa=13
    eee=33
    aac=35




    Thank you
    Last edited by yamaha102; Jun 28th, 2007 at 10:44 AM.

  5. #5
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,628

    Re: (Please help) how to compare certain part between files

    well looks like you could improve it by checking for a [ at the beginning of whatever you read so you know you have hit a new header.
    My light show youtube page (it's made the news) www.youtube.com/@lightsofelberfeld
    Contact me on the socials www.facebook.com/lordorwell

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Jun 2007
    Posts
    92

    Re: (Please help) how to compare certain part between files

    Quote Originally Posted by Lord Orwell
    well looks like you could improve it by checking for a [ at the beginning of whatever you read so you know you have hit a new header.
    i did.
    my problem is... if the [DEF] part are not the same line ..the my previous code failed to work.

  7. #7
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: (Please help) how to compare certain part between files

    Why not just use the INI API's to read the sections and then compare the text inside the sections if you already know the section names.

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Jun 2007
    Posts
    92

    Re: (Please help) how to compare certain part between files

    Quote Originally Posted by randem
    Why not just use the INI API's to read the sections and then compare the text inside the sections if you already know the section names.
    my problem is i dont know how to do the comparison

  9. #9
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,628

    Re: (Please help) how to compare certain part between files

    This code should work. Feel free to modify it to suit (like putting freefile back in it, and naming listboxes correctly)

    Code:
    strHeaderTolookFor1 = "[DEF]"
    
    open "file1" for input as #1
    open "file2" for input as #2
    'seek to first line in file 1
    do 
       if eof(1) then exit do
       line Input #1, File1$
       if instr(file1$ , strheadertolookfor1) then 
          File1Found = true
          exit do
       end if 
    loop
    do 
       if eof(2) then exit do
       line Input #2, File2$
       if instr(file2$ , strheadertolookfor1) then 
          File2Found = true
          exit do
       end if 
    loop
    
    'compareloop
    do
       if eof(1) then enderfile1 = true
       if eof(2) then enderfile2 = true
       if  EnderFileOne = false then Input #1, String1$ 
       if  EnderFiletwo = false then Input #2, string2$ 
       if left(string1, "[") = true then enderfile1 = true
       if left(string2, "[") = true then enderfile2 = true
       if string1$ <> string2$ then
          if enderfile1 = false then listbox1.additem(string1$)
          if enderfile2 = false then listbox2.additem(string2$)
       end if
       
       if enderfile1 = true and enderfile2 = true then exit do
    loop
    close #1: close #2
    My light show youtube page (it's made the news) www.youtube.com/@lightsofelberfeld
    Contact me on the socials www.facebook.com/lordorwell

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Jun 2007
    Posts
    92

    Re: (Please help) how to compare certain part between files

    Hi Lord Orwell,
    thanks for your help.

    Code:
    Private Sub Command1_Click()
    strheadertolookfor1 = "[DEF]"
    
    
    Open "E:\abc.txt" For Input As #1
    Open "E:\abc1.txt" For Input As #2
    'seek to first line in file 1
    Do
       If EOF(1) Then Exit Do
       Line Input #1, file1$
       If InStr(file1$, strheadertolookfor1) Then
          File1Found = True
          Exit Do
       End If
    Loop
    Do
       If EOF(2) Then Exit Do
       Line Input #2, file2$
       If InStr(file2$, strheadertolookfor1) Then
          File2Found = True
          Exit Do
       End If
    Loop
    Dim enderfile1, enderfile2 As Boolean
    
    'compareloop
    Do
       If EOF(1) Then enderfile1 = True
       If EOF(2) Then enderfile2 = True
    If enderfile1 = False Then Input #1, string1$
    If enderfile2 = False Then Input #2, string2$
       If Left(string1, "[") = True Then enderfile1 = True
       If Left(string2, "[") = True Then enderfile2 = True
       
       If string1$ <> string2$ Then
          If enderfile1 = False Then List1.AddItem (string1$)
          If enderfile2 = False Then List2.AddItem (string2$)
       End If
       
       If enderfile1 = True And enderfile2 = True Then Exit Do
    Loop
    Close #1: Close #2
    End Sub
    when i try to run the code, it show an error (run-time error 13 . type mismatch at If Left(string1, "[") = True Then enderfile1 = True
    i couldnt figure out what is the cause.
    i need your continuous support. thanks

  11. #11
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,628

    Re: (Please help) how to compare certain part between files

    that was vb6 code, sorry. It should be this: Microsoft.visualbasic.left(
    otherwise vb.net thinks you are referring to the form.
    My light show youtube page (it's made the news) www.youtube.com/@lightsofelberfeld
    Contact me on the socials www.facebook.com/lordorwell

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Jun 2007
    Posts
    92

    Re: (Please help) how to compare certain part between files

    Quote Originally Posted by Lord Orwell
    that was vb6 code, sorry. It should be this: Microsoft.visualbasic.left(
    otherwise vb.net thinks you are referring to the form.

    i'm getting confused.
    i'm using VB6.

    however i figure it out.. If Left(string1, 1) = "[ Then enderfile1 = True
    thanks for your great hep
    but the code got some problem as i test it out...

    if file 1

    [DEF]
    <--- a blank line
    aaa=13
    eee=33
    aac=3


    [DEF]
    aaa=13 <--- no blank line
    eee=24
    aac=3



    the above code cannot work as it shows all content are diff in fact only eee is diff
    Last edited by yamaha102; Jun 29th, 2007 at 12:34 PM.

  13. #13
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,628

    Re: (Please help) how to compare certain part between files

    heh heh heh ignore my last post. What i actually did was try to use Left like it was Instr. Thats what i get for writing answers to 4 posts at the same time. First i thought your window said vb.net, etc. etc.... I give good code, Usually
    My light show youtube page (it's made the news) www.youtube.com/@lightsofelberfeld
    Contact me on the socials www.facebook.com/lordorwell

  14. #14
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: (Please help) how to compare certain part between files

    You could parse the INI into classes and create methods to compare the classes (and contained classes).

  15. #15
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,628

    Re: solved: how to compare certain part between files

    how about a code sample?
    My light show youtube page (it's made the news) www.youtube.com/@lightsofelberfeld
    Contact me on the socials www.facebook.com/lordorwell

  16. #16

    Thread Starter
    Lively Member
    Join Date
    Jun 2007
    Posts
    92

    Re: Problem AGAIN: how to compare certain part between files

    Hi Lord Orwell.. trouble you again.
    Because i wish to compare ABC and DEF part.. so i use the loop.. but how come my loop cannot work??
    is it i cannot do the loop like that? please guide.

    Code:
    Dim intI As Integer
    For intI = 1 To 2 Step 1
    
    If intI = 1 Then
    strheadertolookfor1 = "[DEF]"
    strheadertolookfor2 = "["
    End If
    
    If intI = 2 Then
    strheadertolookfor1 = "[ABC]"
    strheadertolookfor2 = "["
    End If
    
    
    
    open "file1" for input as #1
    open "file2" for input as #2
    'seek to first line in file 1
    do 
       if eof(1) then exit do
       line Input #1, File1$
       if instr(file1$ , strheadertolookfor1) then 
          File1Found = true
          exit do
       end if 
    loop
    do 
       if eof(2) then exit do
       line Input #2, File2$
       if instr(file2$ , strheadertolookfor1) then 
          File2Found = true
          exit do
       end if 
    loop
    
    'compareloop
    do
       if eof(1) then enderfile1 = true
       if eof(2) then enderfile2 = true
       if  EnderFileOne = false then Input #1, String1$ 
       if  EnderFiletwo = false then Input #2, string2$ 
       if left(string1, "[") = true then enderfile1 = true
       if left(string2, "[") = true then enderfile2 = true
       if string1$ <> string2$ then
          if enderfile1 = false then listbox1.additem(string1$)
          if enderfile2 = false then listbox2.additem(string2$)
       end if
       
       if enderfile1 = true and enderfile2 = true then exit do
    loop
    
    Next IntI
    close #1: close #2

  17. #17
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,628

    Re: Problem again (regarding loop): how to compare certain part between files

    because the headers could be at different lines in the code, and could contain different numbers of entries. If you loop until you are in the section of both of them, you might not even detect a change with your code because here's why

    file 1 file 2
    [header1] ...
    ... ...
    ... ...
    [header2] ...
    ... [header1]
    ... ...
    your code would exit the first header in file 1 before it is even reached in file 2.
    My code seeks to the beginning of each.

    Note that that method isn't foolproof either. A better way would be to store the entire contents of each of them in 2 string arrays and compare line by line to every line in the other one. This way it won't matter if they are mixed up or not.
    My light show youtube page (it's made the news) www.youtube.com/@lightsofelberfeld
    Contact me on the socials www.facebook.com/lordorwell

  18. #18

    Thread Starter
    Lively Member
    Join Date
    Jun 2007
    Posts
    92

    Re: Problem again (regarding loop): how to compare certain part between files

    Quote Originally Posted by Lord Orwell
    because the headers could be at different lines in the code, and could contain different numbers of entries. If you loop until you are in the section of both of them, you might not even detect a change with your code because here's why


    your code would exit the first header in file 1 before it is even reached in file 2.
    My code seeks to the beginning of each.

    Note that that method isn't foolproof either. A better way would be to store the entire contents of each of them in 2 string arrays and compare line by line to every line in the other one. This way it won't matter if they are mixed up or not.
    can guide how to store the entire contents of each of them in 2 string arrays and compare line by line to every line in the other one.
    i am confused.
    thanks

  19. #19
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Problem again (regarding loop): how to compare certain part between files

    You have to compare by logical sections to minimize comparison. Otherwise a single line inserted at the start would affect everything else that follows.

  20. #20

    Thread Starter
    Lively Member
    Join Date
    Jun 2007
    Posts
    92

    Re: Problem again (regarding loop): how to compare certain part between files

    Quote Originally Posted by leinad31
    You have to compare by logical sections to minimize comparison. Otherwise a single line inserted at the start would affect everything else that follows.

    yup that's right..
    So how to do it?
    thanks

  21. #21
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,628

    Re: Problem again (regarding loop): how to compare certain part between files

    I apologize. I didn't actually look at that code closely until just now. Now i see what you are trying to do.
    the problem is that your variables are all set in the first loop, so it just exits during the 2nd one.
    Right before the OPEN commands put this:
    Code:
    enderfile1 = false:  enderfile2 = false:
    file1found = false: file2found = false
    My light show youtube page (it's made the news) www.youtube.com/@lightsofelberfeld
    Contact me on the socials www.facebook.com/lordorwell

  22. #22

    Thread Starter
    Lively Member
    Join Date
    Jun 2007
    Posts
    92

    Re: Problem again (regarding loop): how to compare certain part between files

    Quote Originally Posted by Lord Orwell
    I apologize. I didn't actually look at that code closely until just now. Now i see what you are trying to do.
    the problem is that your variables are all set in the first loop, so it just exits during the 2nd one.
    Right before the OPEN commands put this:
    Code:
    enderfile1 = false:  enderfile2 = false:
    file1found = false: file2found = false
    thank you so much

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