/build/static/layout/Breadcrumb_cap_w.png

compare text file contents

Hi,
I have 2 text files which contains a list of 500 computer names. How can i
run a script to compare the contents and list out the differences between
the 2 files?

sample content of the text file:
computer1
computer2
computer3
computer4
.... and so on....

pls advice. thanks

0 Comments   [ + ] Show comments

Answers (2)

Posted by: cuallen 21 years ago
Senior Yellow Belt
2
Are you wanting the differences of both files to be listed or do you only want the difference from one file list using the other file as a master list? I may have a script that will work for this purpose. I'm not much of a programmer but I know my VB Scripting language fairly well.
Posted by: cuallen 21 years ago
Senior Yellow Belt
2
Copy the below code into a test file and rename it's extension to .vbs

This script will compare two text files to one another and output the differences into a third text file. You'll want to change the paths of the files and the file names. (i.e C:\file1.txt)
___________________________________________________
Const ForAppending = 8
Const ForWriting = 2
Const ForReading=1

Set objFSO = CreateObject("Scripting.FileSystemObject")

'Source file #1. Change the path and file name.
Set objFile1 = objFSO.OpenTextFile("C:\file1.txt", ForReading)
'Reads the entire contents of File1 into a string variable.
sContents1=objFile1.ReadAll
objFile1.Close

'Source file #2. Change the path and file name.
Set objFile2 = objFSO.OpenTextFile("C:\file2.txt", ForReading)
'Reads the entire contents of File2 into a string variable.
sContents2=objFile2.ReadAll
objFile2.Close

'Output file, contains the list of differences between Source file #1 and Source file #2. Change the path and file name.
'Warning! This will overwrite File3 everytime the script is run. If the output file (File3) isn't found, then it will be created.
If objFSO.FileExists("C:\file3.txt") Then
Set objFile3 = objFSO.OpenTextFile("C:\file3.txt", ForWriting)
Else
Set objFile3 = objFSO.CreateTextFile("C:\file3.txt")
End If

Set objFile1 = objFSO.OpenTextFile("C:\file1.txt", ForReading)
'Reads each line of File1 and searches for a match in the variable containing the contents of File2
Do Until objFile1.AtEndOfStream
strComputer = objFile1.ReadLine
'If the line from File1 can't be found in File2, then it will right the line to File3
If InStr(sContents2, strComputer) = 0 then
objFile3.WriteLine (strComputer)
End If
'Clears the variable
strComputer = ""
Loop
objFile1.Close

Set objFile2 = objFSO.OpenTextFile("C:\file2.txt", ForReading)
'Reads each line of File2 and searches for a match in the variable containing the contents of File1
Do Until objFile2.AtEndOfStream
strComputer = objFile2.ReadLine
'If the line from File2 can't be found in File1, then it will right the line to File3
If InStr(sContents1, strComputer) = 0 then
objFile3.WriteLine (strComputer)
End If
'Clears the variable
strComputer = ""
Loop

'Closes the open files
objFile2.Close
objFile3.Close
Rating comments in this legacy AppDeploy message board thread won't reorder them,
so that the conversation will remain readable.

Don't be a Stranger!

Sign up today to participate, stay informed, earn points and establish a reputation for yourself!

Sign up! or login

View more:

Share

 
This website uses cookies. By continuing to use this site and/or clicking the "Accept" button you are providing consent Quest Software and its affiliates do NOT sell the Personal Data you provide to us either when you register on our websites or when you do business with us. For more information about our Privacy Policy and our data protection efforts, please visit GDPR-HQ