Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.


Compare 2 files and only output differences?

Compare 2 files and only output differences?

I have a master list of machine names. I then need to modify these machines in some ways, sometimes patch, sometimes change settings, basic administrative things. This leaves me with an output of the ones patched and ones not. (Its alot more complicated than that as I get a bunch of various statuses I am just making a generic example).

I want to be able to compare the master list vrs the ones successfull and have it output all the machine named from the master list that isn't in this secondary list.

Is there an easy way to do this?

Master.txt has a computer name per line -

Finished.txt has the successes in the same format -

I want Still remaining.txt to be created and just have -

(I bet you can't tell who is hungry?)

Any quick scripting way to do this?

RE: Compare 2 files and only output differences?

read the contents of first file into a dictionary object with the machine name as the key and item = "null"
read the contents of the second file and for each machine name do a

Do While Not tsFinished.AtEndOfStream
   strMachine = ""
   strMachine = tsFinished.ReadLine
   If dicMachines.Exists(strMachine) Then
     dicMachines.Item(strMachine) = "finished"
   End If

   For Each aMachine In dicMachines
      If dicMachines.Item(aMachine) <> "finished" Then
           tsOutputFile.WriteLine aMachine
      End If
you will prob want to worry about UCase() and uniqueness but you get the idea

RE: Compare 2 files and only output differences?

I just whipped this up real quick.  This should work for you.


'* Author: djtech2k        *
'* Date: 1-31-2007                                   *
'* Script: Reads source text line by line and looks  *
'* for match in text file 2. Outputs NON-matches in  *
'* text file 3.                                      *

Option Explicit

On Error Resume Next

Dim fs, fs2, fs3, objTextFile, objtextFile2, objTextFile3
Dim strSearchString, strText0, arrStr

Const ForReading = 1, ForWriting = 2, ForAppending = 8

set fs=CreateObject("Scripting.FileSystemObject")
set objTextFile = fs.OpenTextFile("c:\source.txt", ForReading)

set fs2=CreateObject("Scripting.FileSystemObject")
set objTextFile2 = fs2.OpenTextFile("c:\check.txt", ForReading)
strSearchString = objTextFile2.ReadAll

set fs3=CreateObject("Scripting.FileSystemObject")
set objTextFile3 = fs3.OpenTextFile("c:\whats_left.txt", ForAppending, True)

Do Until objTextFile.AtEndOfStream
  arrStr = split(objTextFile.ReadLine,vbcrlf)
  strText0 = arrStr(0)

If strText0 <> "" Then
If InStr(strSearchString,strText0) Then
End If
End If


'Clean Up

set objTextFile = Nothing
set objTextFile2 = Nothing
set objTextFile3 = Nothing

set fs = Nothing
set fs2 = Nothing
set fs3 = Nothing

Let me know how it works for you.

RE: Compare 2 files and only output differences?


RE: Compare 2 files and only output differences?

You know I was wondering what the "pluto" and "pluto1" meant and then it hit me, instr will not distinguish between the two.  I agree with mrmovie about using a dictionary. This is what I had in mind, pretty much the same as mrmovie's approach.


Option Explicit
'On Error Resume Next

Dim objFSO, objFile, objFile1, objDict, strLine

Const ForReading = 1
Const ForAppending = 8

Set objDict = CreateObject("Scripting.Dictionary")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile1 = objFSO.OpenTextFile("c:\temp\remaining.txt", ForAppending, True)

Set objFile = objFSO.OpenTextFile("c:\temp\finished.txt", ForReading)
Do Until objFile.AtEndOfStream
    strLine = objFile.ReadLine
    If Not objDict.Exists(strLine) Then
        objDict.Add strLine, ""
    End If         

Set objFile = objFSO.OpenTextFile("c:\temp\master.txt", ForReading)
Do Until objFile.AtEndOfStream
    strLine = objFile.ReadLine
    If Not objDict.Exists(strLine) Then
         objfile1.WriteLine strLine
    End If

My philosophy: K.I.S.S - Keep It Simple Stupid

RE: Compare 2 files and only output differences?

I know exactly what you are saying.  My method is just searching for a string and will accept anything that has that string embedded in it.  From the original post, it sounds like these are computer names.  I would assume under normal circumstances that machine names will be unique.  Assuming that and that the original post did not say that a requirement was the uniqueness, thats why I posted what I did.

If the "pluto" and "pluto1" scenario is a possibility in a list of computer names then a dictionary would be the most exact way to do it.

RE: Compare 2 files and only output differences?

the "(Its alot more complicated than that as I get a bunch of various statuses I am just making a generic example)."
lead me to think ST would want more info later?

perhaps sticking with the dictionary would lead to the key = machinename and the item = a machine class with properties set depending on reading lots of different disparate info then outputing in various ways?

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close