Wrox Programmer Forums Compare 2 Text Files
 Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
 VBScript For questions and discussions related to VBScript.
 Welcome to the p2p.wrox.com Forums. You are currently viewing the VBScript section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
December 20th, 2004, 12:24 PM
 Registered User Join Date: Dec 2004 Location: , , . Posts: 1 Thanks: 0 Thanked 0 Times in 0 Posts
Compare 2 Text Files

What I would like to do is compare two text files and output differences to a third text file.  If the entry from the txtFile1.txt is not in txtFile2.txt write line to txtFile3.txt.  txtFile1.txt will have about 1800 lines and txtFile2.txt will have about 400-900 lines.
Code:
Const ForReading = 1, ForWriting = 2
Dim fso, txtFile, txtFile2, strLine1, strLine2, strMatch
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile("c:\results.txt", ForWriting, True)

Do Until txtFile1.AtEndOfStream
Do Until txtFile2.AtEndOfStream
If Trim(UCase(strLine2)) = Trim(UCase(strLine1)) Then
strMatch = True
Else  Removed
f.WriteLine strLine1 & "    " & strLine2   Removed
End If
Loop
If strMatch <> True then
f.writeline strLine1
End If
Loop
f.Close
Wscript.Echo "Done"
The output from this is the first line from txtFile1.txt and every line from txtFile2.txt.

Any help would be greatly appreciated.

Mike

**EDIT**
I think that I may have fixed one of the errors in my logic.(changes in bold)  However, I am still having problems.
December 20th, 2004, 04:02 PM
Friend of Wrox
 Points: 9,611, Level: 42
 Activity: 0%

Join Date: Mar 2004
Location: Washington, DC, USA.
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts

It seems that what you are trying to do with your flow control is that you are reading a single line from txtFile.1, and then reading every line from txtFile2.txt to compare them, and then moving on to the next line in txtFile1.txt, and comparing it to every line in txtFile2.txt, etc.

But the way this is looping through, it is reading Line1 from text file 1, then all the lines in text file 2, then the remaining lines from text file 1.

The reason it is doing this is that after it loops through all the lines in text file 2, it IS AtEndOfStream, so it won't loop through subsequent iterations. The boolean is set to True. No more looping.

You need to reset the inner loop for every iteration of the outer loop.

THEN, your variable checking is wrong.

Then, you're actually asking it to print a line from the first text file, and a line from the second text file when the lines don't match, and they don't match in every case but one, so you will get a very large output in the results.txt file.

I have no idea what this "Removed" is for:
'=================================
Else Removed
f.WriteLine strLine1 & " " & strLine2 Removed
'=================================

I rewrote it like this to get it to work:
'=================================
Else f.WriteLine strLine1 & " " & strLine2
'=================================

I hope this gets you started. I am not sure why you are using Trim instead of InStr unless the data is not very unique in every line.

As an alternative, consider pumping this through an Access database, setting up a query to find unmatched lines, then pumping out the dynaset to your text file. That would be easier for me.

mmcdonal
December 28th, 2004, 04:12 PM
Friend of Wrox
 Points: 9,611, Level: 42
 Activity: 0%

Join Date: Mar 2004
Location: Washington, DC, USA.
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts

To do this you need to do the following:

1. Create an Access database called Comparo.mdb (or another name) with two tables named tblTextFile1 and tblTextFile2.
The tables should have two fields per record:
a. LineID (autonumber PK)
b. Line (memo)

2. Also in the database create a find unmatched query to find the difference between these two tables and call this qryTextFile3

4. This assumes your two text files are on your C: root.

Then use this code to pull the data from your text files and push them into your database tables:

'========Start Code==========================
Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FileExists("C:\text1.txt") Then
Set objStream = fso.OpenTextFile("C:\text1.txt", 1, False, 0)
End If

Do While Not objStream.AtEndOfStream
strLine = objStream.ReadLine 'captures the whole line

objConn.Open "DSN=Comparo;"
'objRS.CursorLocation = 3
objRS.Open "SELECT * FROM tblTextFile1", objConn, 3, 3
objRS("Line") = strLine
objRS.Close
objConn.Close
Loop

Set fso = CreateObject("Scripting.FileSystemObject")
Set MyFile = fso.CreateTextFile("c:\text1.txt", True)

Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FileExists("C:\text2.txt") Then
Set objStream = fso.OpenTextFile("C:\text2.txt", 1, False, 0)
End If

Do While Not objStream.AtEndOfStream
strLine = objStream.ReadLine 'captures the whole line

objConn.Open "DSN=Comparo;"
'objRS.CursorLocation = 3
objRS.Open "SELECT * FROM tblTextFile2", objConn, 3, 3
objRS("Line") = strLine
objRS.Close
objConn.Close
Loop

Set fso = CreateObject("Scripting.FileSystemObject")
Set MyFile = fso.CreateTextFile("c:\text2.txt", True)

'===========End Code=======================

This will pull the data into your access database and through the query where you will find your results. It also overwrites the text files so they are empty afterwards.

I am not sure why or how you want this data to show up in a third text file. Can you explain the results you want to see and the format?

P.S. - It is a little cludgey in that I don't think you may have to open and close the database connection so many times. But with this small amount of data, I think it should work in less than a minute.

I hope this helps.

mmcdonal
January 15th, 2005, 01:06 PM
 Registered User Join Date: Jan 2005 Location: E Peoria, IL, USA. Posts: 7 Thanks: 0 Thanked 0 Times in 0 Posts

Hello,

What other problems are you having.

could you update the original post so we are all on the same page

Thanks

Randy Dray
August 21st, 2007, 05:21 PM
 Registered User Join Date: Aug 2007 Location: , , . Posts: 1 Thanks: 0 Thanked 0 Times in 0 Posts

You don't really need to create an access database. The code can work like you have it if you don't mind it being a little inefficient.

Here it is modified a little bit so that the logic works properly.

Const ForReading = 1, ForWriting = 2
Dim fso, txtFile, txtFile2, strLine1, strLine2, strMatch
Set fso = CreateObject("Scripting.FileSystemObject")

Do Until txtFile1.AtEndOfStream
strMatch = False
Do Until txtFile2.AtEndOfStream
If Trim(UCase(strLine2)) = Trim(UCase(strLine1)) Then
strMatch = True
Else
End If
Loop
txtFile2.Close
If strMatch <> True then
f.writeline strLine1
End If
Loop
f.Close
Wscript.Echo "Done"

You can see that I modifed the variable names.......i need the code too. :D
April 16th, 2016, 10:35 AM
Registered User
 Points: 3, Level: 1
 Activity: 0%

Join Date: Apr 2016
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks

After about 10 years, it helps me ;) Thanks Man !!

 Thread Tools Display Modes Linear Mode

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is Off HTML code is OffTrackbacks are Off Pingbacks are On Refbacks are Off

 Similar Threads Thread Thread Starter Forum Replies Last Post compare 2 files and output third files mtnguye6 VBScript 0 April 14th, 2008 12:03 PM Source on Java to compare XML files like ExamXML alapick XML 1 July 28th, 2006 05:05 AM Compare two xml files using xslt sudha XSLT 0 March 10th, 2006 01:04 AM Is there any tool available to compare 2 xml files parekh_bhakti ASP.NET 1.0 and 1.1 Professional 1 October 28th, 2005 01:59 PM compare two xml files attributes, elements k_s_ashok XML 1 June 9th, 2004 03:08 AM

All times are GMT -4. The time now is 01:38 PM.