Smart questions
Smart answers
Smart people
Join Tek-Tips Forums
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login




Remember Me
Forgot Password?
Join Us!

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • 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!

Join Tek-Tips
*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.
Jobs from Indeed

Link To This Forum!

Partner Button
Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Compare 2 files and only output differences?

StumpedTechy (MIS) (OP)
31 Jan 07 8:06
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?
mrmovie (TechnicalUser)
31 Jan 07 9:10
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
djtech2k (MIS)
31 Jan 07 9:42
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.
mrmovie (TechnicalUser)
31 Jan 07 9:49
PLUTO
PLUTO1
dm4ever (TechnicalUser)
31 Jan 07 10:02
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

djtech2k (MIS)
31 Jan 07 10:14
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.
mrmovie (TechnicalUser)
31 Jan 07 10:20
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?




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!

Back To Forum

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