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
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)
Please log in to answer
Posted by:
cuallen
21 years ago
Posted by:
cuallen
21 years ago
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
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.
so that the conversation will remain readable.