Compare two text files (VBScript)

This code can be found in Chapter 8 of Windows XP Cookbook

Purchase XP Cookbook or Networking Recipes for only $25 plus shipping! While supplies last.

Find out how to download all of the VBScript code from this site.

' From the book "Windows XP Cookbook"
' ISBN: 0596007256

' ------ SCRIPT CONFIGURATION ------
strFile1 = "<FilePath1>" ' e.g. c:\scripts\test1.vbs
strFile2 = "<FilePath2>" ' e.g. c:\scripts\test2.vbs
' ------ END CONFIGURATION ---------
set objFSO = CreateObject("Scripting.FilesystemObject")
set objFile1 = objFSO.opentextfile(strFile1,1)
set objFile2 = objFSO.opentextfile(strFile2,1)
arrFile1 = split(objFile1.ReadAll,vbNewLine)
arrFile2 = split(objFile2.ReadAll,vbNewLine)
objFile1.close
objFile2.close

if ubound(arrFile1) < ubound(arrFile2) then
 intLineCount = ubound(arrFile1)
 strError = strFile2 & " is bigger than " & strFile1
elseif ubound(arrFile1) > ubound(arrFile2) then
 intLineCount = ubound(arrFile2)
 strError = strFile2 & " is bigger than " & strFile1
else 
 intLineCount = ubound(arrFile2)
end if

for i = 0 to intLineCount
 if not arrFile1(i) = arrFile2(i) then 
   exit for
 end if
next

if i < (intLineCount + 1) then
 WScript.Echo "Line " & (i+1) & " not equal"
 WScript.Echo strError
elseif strError <> "" then
 WScript.Echo strError
else 
 WScript.Echo "Files are identical."
end if

This code has been viewed 4823 times.

New from the creators of TechTasks.com: StatSheet.com