Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Other Programming > VBScript
| 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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old December 20th, 2004, 12:24 PM
Registered User
 
Join Date: Dec 2004
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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 txtFile1 = fso.OpenTextFile("c:\txtFile1.txt", ForReading)
Set txtFile2 = fso.OpenTextFile("c:\txtFile2.txt", ForReading)
Set f = fso.OpenTextFile("c:\results.txt", ForWriting, True)

Do Until txtFile1.AtEndOfStream
    strLine1 = txtFile1.Readline
        Do Until txtFile2.AtEndOfStream
            strLine2 = txtFile2.Readline
                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.

Thanks in advance,
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.
Reply With Quote
  #2 (permalink)  
Old December 20th, 2004, 04:02 PM
Friend of Wrox
Points: 9,611, Level: 42
Points: 9,611, Level: 42 Points: 9,611, Level: 42 Points: 9,611, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2004
Location: Washington, DC, USA.
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
Default

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
Reply With Quote
  #3 (permalink)  
Old December 28th, 2004, 04:12 PM
Friend of Wrox
Points: 9,611, Level: 42
Points: 9,611, Level: 42 Points: 9,611, Level: 42 Points: 9,611, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2004
Location: Washington, DC, USA.
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
Default

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

3. A system DSN called "Comparo" linked to your Access database.

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

    Set objConn = CreateObject("ADODB.Connection")
    Set objRS = CreateObject("ADODB.Recordset")
    objConn.Open "DSN=Comparo;"
    'objRS.CursorLocation = 3
    objRS.Open "SELECT * FROM tblTextFile1", objConn, 3, 3
        objRS.AddNew
        objRS("Line") = strLine
           objRS.AddNew
           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

    Set objConn = CreateObject("ADODB.Connection")
    Set objRS = CreateObject("ADODB.Recordset")
    objConn.Open "DSN=Comparo;"
    'objRS.CursorLocation = 3
    objRS.Open "SELECT * FROM tblTextFile2", objConn, 3, 3
        objRS.AddNew
        objRS("Line") = strLine
           objRS.AddNew
           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
Reply With Quote
  #4 (permalink)  
Old 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
Default

Hello,

What other problems are you having.

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

Thanks

Randy Dray
Reply With Quote
  #5 (permalink)  
Old August 21st, 2007, 04:21 PM
Registered User
 
Join Date: Aug 2007
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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")
Set txtFile1 = fso.OpenTextFile("CurrentDIR.txt", ForReading)
Set f = fso.OpenTextFile("Files2Download.txt", ForWriting, True)

Do Until txtFile1.AtEndOfStream
strMatch = False
    strLine1 = txtFile1.Readline
Set txtFile2 = fso.OpenTextFile("LOG.txt", ForReading)
        Do Until txtFile2.AtEndOfStream
            strLine2 = txtFile2.Readline
                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
Reply With Quote
  #6 (permalink)  
Old April 16th, 2016, 09:35 AM
Registered User
Points: 3, Level: 1
Points: 3, Level: 1 Points: 3, Level: 1 Points: 3, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2016
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Thanks

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




Similar Threads
Thread Thread Starter Forum Replies Last Post
compare 2 files and output third files mtnguye6 VBScript 0 April 14th, 2008 11:03 AM
Source on Java to compare XML files like ExamXML alapick XML 1 July 28th, 2006 04: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 12:59 PM
compare two xml files attributes, elements k_s_ashok XML 1 June 9th, 2004 02:08 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.