General discussion

Locked

Report lines NOT in two compared files

By cornejo.alvaro ·
Hi

I'm quite new on this and I need to compare 2 text files that contains several lines and get a report of wich lines are NOT in both files. I've made a vbscript and can get to have wich lines are in both files; however I can't get to make it work for the ones that are not.

I can not just use the negative of the compare since other wise I'll have a entry for each of line that does not match each line on the first file

This is what I got

FUNCTION Compare
SET f1 = fs.OpenTextFile(TxtFile1, FORREADING)
SET rf = fs.OpenTextFile(ResultFile,FORWRITING,TRUE)
SET OrigLine = Nothing
SET CmpLine = Nothing

DO
SET f2 = fs.OpenTextFile(TxtFile2, FORREADING)
OrigLine = f1.Readline
DO UNTIL f2.AtEndOfStream
CmpLine = f2.Readline
IF OrigLine = CmpLine Then
rf.WriteLine OrigLine
END IF
LOOP
f2.Close
LOOP UNTIL f1.AtEndOfStream
rf.Close
f1.Close
END FUNCTION

Any ideas?

Regards

Alvaro

This conversation is currently closed to new comments.

3 total posts (Page 1 of 1)  
| Thread display: Collapse - | Expand +

All Comments

Collapse -

by lowlands In reply to Report lines NOT in two c ...

First, it might be easier to use something that already exists, like the DOS 'FC' command. You could even use wscript.shell to incorporate it in a vbscript if you need to.

But if you really need to use a vbscript, you'll have to run your loops twice.
- open file1, readline1 and compare against all lines in file2. Keep doing this for all lines in file1
- then open file2, readline1 and compare against all lines in file1.

You'll have to do this unless you know for sure which file has the most lines. So I guess you could also check both files and count their lines.

Then you'll want the occasion where OrigLine doesn't equal cmpline.

I would do something like

...
Do until f2.atendofstream
If OrigLine=CmpLine Then
exists = TRUE
Else exists = FALSE
end if
loop
IF exists = true Then
rf.writeline OrigLine

...

Hope this helps, be

Collapse -

by cornejo.alvaro In reply to

Hi Iowlands

Thanks for your reply. It worked fine just that the "EXIT IF" command should be an "EXIT DO"; otherwise, the "exist" condition will be overwritten.

...
Do until f2.atendofstream
If OrigLine=CmpLine Then
exists = TRUE
Else exists = FALSE
END DO
loop
IF exists = true Then
rf.writeline OrigLine

...


Thanks

Alvaro

Collapse -

by cornejo.alvaro In reply to Report lines NOT in two c ...

This question was closed by the author

Back to Web Development Forum
3 total posts (Page 1 of 1)  

Related Discussions

Related Forums