Discussion Locked

Report lines NOT in two compared files

0 helpful
3 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

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

3 replies

by lowlands

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

by cornejo.alvaro

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

This discussion is closed to new replies.