VB Script - Compare two text files

There are multiple ways to compare files but i would like to publish that i found most comprehensive one.This one I got from HP reference and really like it.Also well tested and established method.

' *********************************************************************************************
' Compare two text files.
' Parameters:
' FilePath1 - location of the first file to be compared
' FilePath2 - location of the second file to be compared
' FilePathDiff - location of the diffrences file
' ignoreWhiteSpace - controls whether or not to ignore differences in whitespace characters
' true - ignore differences in whitespace
' false - do not ignore difference in whitespace
' Return Value: true if files are identical, false otherwise'
' *********************************************************************************************
Function FileCompare (byref FilePath1, byref FilePath2, byref FilePathDiff, ignoreWhiteSpace)
    dim differentFiles
    differentFiles = false

    dim f1, f2, f_diff
    ' open the files
    set f1 = OpenFile(FilePath1,1)
    set f2 = OpenFile(FilePath2,1)
    set f_diff = OpenFile(FilePathDiff,8)

    dim rowCountF1, rowCountF2
    rowCountF1 = 0
    rowCountF2 = 0

    dim str
    ' count how many lines there are in first file
    While not f1.AtEndOfStream
        str = ReadLineFromFile(f1)
        rowCountF1= rowCountF1 + 1

    ' count how many lines there are in second file
    While not f2.AtEndOfStream
        str = ReadLineFromFile(f2)
        rowCountF2= rowCountF2 + 1

    ' re-open the files to go back to the first line in the files
    set f1 = OpenFile(FilePath1,1)
    set f2 = OpenFile(FilePath2,1)

    ' compare the number of lines in the two files.
    ' assign biggerFile - the file that contain more lines
    ' assign smallerFile - the file that contain less lines
    dim biggerFile, smallerFile
    set biggerFile = f1
    set smallerFile = f2
    If ( rowCountF1 < rowCountF2) Then
        set smallerFile = f1
        set biggerFile = f2
    End If

    dim lineNum,str1, str2
    lineNum = 1
    str = "Line" & vbTab & "File1" & vbTab & vbTab & "File2"
    WriteToFile f_diff,str
     ' loop on all the lines in the samller file
    While not smallerFile.AtEndOfStream
        ' read line from both files
        str1 = ReadLineFromFile(f1)
        str2 = ReadLineFromFile(f2)

        ' check if we need to ignore white spaces, if yes, trim the two lines
        If Not ignoreWhiteSpace Then
        End If

        ' if there is a diffrence between the two lines, write them to the diffrences file
        If not (str1 = str2) Then
            differentFiles = true
            str = lineNum & vbTab & str1 & vbTab & vbTab & str2
            WriteToFile f_diff,str
        End If
        lineNum = lineNum + 1

    ' loop on the bigger lines, to write its line two the diffrences file
    While not biggerFile.AtEndOfStream
        str1 = ReadLineFromFile(biggerFile)
        str = lineNum & vbTab & "" & vbTab & vbTab & str2
        WriteToFile f_diff,str
        lineNum = lineNum + 1
    FileCompare = Not differentFiles
End function

No comments:

Post a Comment