Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Compare 2 files and only output differences?

Status
Not open for further replies.
Feb 11, 2005
153
US
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?

E.G.
Master.txt has a computer name per line -
Apple
Banana
Orange
Pear

Finished.txt has the successes in the same format -
Apple
Orange

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

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

Any quick scripting way to do this?
 
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
Next
Loop
you will prob want to worry about UCase() and uniqueness but you get the idea
 
I just whipped this up real quick. This should work for you.

Code:
'*****************************************************
'* Author: djtech2k sportsfan@teamarsenal.net        *
'* 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
Else
objTextFile3.WriteLine(strText0)
End If
End If


Loop

'Clean Up
objTextFile.Close
objTextFile2.Close
objTextFile3.Close

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.
 
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.

Code:
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 		
Loop
objFile.Close

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
Loop
objFile.Close

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
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.
 
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?




 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top