Can i get one vbscript to get the difference between two text files and write it in 3rd text file?
Hi
I want a vbscript which will output the difference between two text files and write the difference in 3rd text file. Please help.
5 Comments
[ + ] Show comments
Answers (1)
Please log in to answer
Posted by:
flip1001
10 years ago
Here is my attempt. I based it from code from
http://www.perlmonks.org/?node_id=1016585 and http://blogs.technet.com/b/heyscriptingguy/archive/2007/05/24/how-can-i-compare-the-contents-of-two-text-files.aspx
Option Explicit
On Error Resume Next
Const ForReading = 1
Const TextCompare = 1
Dim File1, File2, OutputFile
File1 = "file1.txt"
File2 = "file2.txt"
OutputFile = "outfile.txt"
Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
If ObjFSO.FileExists(File1) Then
Dim objFile1 : Set objFile1 = objFSO.OpenTextFile("file1.txt", ForReading)
Else
WScript.Quit
End If
' Dictionary object for reference file.
Dim RefDict : Set RefDict = CreateObject("Scripting.Dictionary")
RefDict.CompareMode = TextCompare
Dim StrLine, SearchLine, strNotFound
' Read reference file into dictionary object.
Do Until objFile1.AtEndOfStream
StrLine = Trim(objFile1.ReadLine)
if Not RefDict.Exists(StrLine) Then
RefDict.Add StrLine, "1"
End If
Loop
objFile1.Close
' File that may have more or less lines.
If ObjFSO.FileExists(File2) Then
Dim objFile2 : Set objFile2 = objFSO.OpenTextFile("file2.txt", ForReading)
Else
WScript.Quit
End If
' Search 2nd file with reference file.
Do Until objFile2.AtEndOfStream
SearchLine = Trim(objFile2.ReadLine)
If Not RefDict.Exists(SearchLine) Then
If IsEmpty(strNotFound) Then
strNotFound = SearchLine
Else
strNotFound = strNotFound & vbCrLf & SearchLine
End If
End If
Loop
objFile2.Close
If IsEmpty(strNotFound) or strNotFound = "" Then
WScript.Quit
End If
Dim objFile3 : Set objFile3 = objFSO.CreateTextFile(OutputFile, True)
objFile3.WriteLine strNotFound
objFile3.Close
Use Beyond Compare. It has a CLI and can thus be driven by script. - anonymous_9363 10 years ago