|
-
Jun 26th, 2007, 09:38 PM
#1
Thread Starter
Lively Member
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.
-
Jun 28th, 2007, 01:54 AM
#2
Thread Starter
Lively Member
Re: (help)compare certain part in file
-
Jun 28th, 2007, 02:01 AM
#3
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?
-
Jun 28th, 2007, 08:50 AM
#4
Thread Starter
Lively Member
Re: (help)compare certain part in file
 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.
-
Jun 28th, 2007, 01:05 PM
#5
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.
-
Jun 28th, 2007, 07:06 PM
#6
Thread Starter
Lively Member
Re: (Please help) how to compare certain part between files
 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.
-
Jun 28th, 2007, 11:37 PM
#7
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.
-
Jun 29th, 2007, 12:27 AM
#8
Thread Starter
Lively Member
Re: (Please help) how to compare certain part between files
 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
-
Jun 29th, 2007, 08:27 AM
#9
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
-
Jun 29th, 2007, 11:42 AM
#10
Thread Starter
Lively Member
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
-
Jun 29th, 2007, 12:00 PM
#11
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.
-
Jun 29th, 2007, 12:06 PM
#12
Thread Starter
Lively Member
Re: (Please help) how to compare certain part between files
 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.
-
Jun 29th, 2007, 07:17 PM
#13
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
-
Jun 30th, 2007, 02:01 AM
#14
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).
-
Jun 30th, 2007, 10:13 AM
#15
Re: solved: how to compare certain part between files
-
Jul 1st, 2007, 07:54 AM
#16
Thread Starter
Lively Member
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
-
Jul 1st, 2007, 08:30 AM
#17
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.
-
Jul 1st, 2007, 07:04 PM
#18
Thread Starter
Lively Member
Re: Problem again (regarding loop): how to compare certain part between files
 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
-
Jul 1st, 2007, 07:26 PM
#19
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.
-
Jul 1st, 2007, 08:00 PM
#20
Thread Starter
Lively Member
Re: Problem again (regarding loop): how to compare certain part between files
 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
-
Jul 1st, 2007, 08:02 PM
#21
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
-
Jul 1st, 2007, 08:33 PM
#22
Thread Starter
Lively Member
Re: Problem again (regarding loop): how to compare certain part between files
 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|