VBScript to compare different file types

'Comparing Text, XML, and HTML files

'Close all open Notepad files
Call CloseAllNotepadFiles()

''Close all open HTML pages
'Call CloseAllHTMLDocuments()

'Get the path of current directory
FilePath = CreateObject("Scripting.FileSystemObject").GetAbsolutePathName(".")

'Get files to comapare
f1 = FilePath &"\files\File1.txt"
f2 = FilePath &"\files\File2.txt"

''Get files to comapare
'File1 = FilePath &"\files\File1.xml"
'File2 = FilePath &"\files\File2.xml"

''Get files to comapare
'File1 = FilePath &"\files\File1.html"
'File2 = FilePath &"\files\File2.html"

'Call CompareFiles function
Call Comparefiles(f1,f2)
If CompareFiles(f1, f2) = False Then
    MsgBox "Files are identical."
Else
    MsgBox "Files are different."
End If

'CompareFiles function
Public Function CompareFiles (FilePath1, FilePath2)
Dim FS, File1, File2
Set FS = CreateObject("Scripting.FileSystemObject")

If FS.GetFile(FilePath1).Size <> FS.GetFile(FilePath2).Size Then
    CompareFiles = True
    Exit Function
End If
Set File1 = FS.GetFile(FilePath1).OpenAsTextStream(1, 0)
Set File2 = FS.GetFile(FilePath2).OpenAsTextStream(1, 0)

CompareFiles = False
Do While File1.AtEndOfStream = False

    ' To compare files Word-By-Word - It takes time
    'Str1 = File1.Read (1)
    'Str2 = File2.Read (1)
   
    'To compare files at once - copies entire text of file in one variable
    'Str1 = File1.ReadAll
    'Str2 = File2.ReadAll
   
    'To compare the file Line-By-Line - Use this method
    Str1 = File1.Readline
    Str2 = File2.Readline

CompareFiles = StrComp(Str1, Str2, 0)

    If CompareFiles <> 0 Then
    CompareFiles = True
Exit Do
    End If
Loop

File1.Close()
File2.Close()
Set File1 = Nothing
Set File2 = Nothing
Set FS = Nothing
End Function

'Close all open Notepad files
Function CloseAllNotepadFiles()
Dim WshShell, oExec
Set WshShell = CreateObject("WScript.Shell")
Set oExec = WshShell.Exec("taskkill /f /im notepad.exe")
WScript.sleep 2000
Set WshShell = Nothing
Set oExec = Nothing
End Function

''Close all open HTML pages
'Function CloseAllHTMLDocuments()
'Dim WshShell, oExec
'Set WshShell = CreateObject("WScript.Shell")
'Set oExec = WshShell.Exec("taskkill /f /im iexplore.exe")
'WScript.sleep 2000
'Set WshShell = Nothing
'Set oExec = Nothing
'End Function

'############################################################################

'Compare Excel files

'Close all open Excel documents
Call CloseAllExcelDocuments()

'Get the path of current directory
FilePath = CreateObject("Scripting.FileSystemObject").GetAbsolutePathName(".")

'Get files to comapare
File1 = FilePath &"\files\File1.xlsx"
File2 = FilePath &"\files\File2.xlsx"

'Call ComapareXLSFiles function
Call ComapareXLSFiles(File1,File2)
If ComapareXLSFiles(File1,File2) = True Then
    MsgBox "Files are identical."
Else
    MsgBox "Files are different."
End If

'ComapareXLSFiles function
Function ComapareXLSFiles(File1,File2)
    Dim objExcel
    Dim objSheet       
    set objExcel1 = Createobject("Excel.Application")       
    objExcel1.Workbooks.open File1
    set objExcel2 = Createobject("Excel.Application")
    objExcel2.Workbooks.open File2   
    Set objSheet1 = objExcel1.ActiveWorkbook.Worksheets("Sheet1")
    Set objSheet2 = objExcel2.ActiveWorkbook.Worksheets("Sheet1")
    For Each cell In objSheet1.UsedRange
       If cell.Value <> objSheet2.Range(cell.Address).Value Then
            Flag = False
            Exit for
       Else
            Flag = True
       End If
    Next   
    objExcel1.Workbooks.close
    objExcel2.Workbooks.close   
    set objExcel1 = Nothing
    set objExcel2 = Nothing
    set objSheet1 = Nothing
    set objSheet2 = Nothing
    set objExcel = Nothing
ComapareXLSFiles = Flag
End Function

'Close all open Excel documents
Function CloseAllExcelDocuments()
Dim WshShell, oExec
Set WshShell = CreateObject("WScript.Shell")
Set oExec = WshShell.Exec("taskkill /f /im excel.exe")
WScript.sleep 2000
Set WshShell = Nothing
Set oExec = Nothing
End Function

'############################################################################

'Compare XML files 

'Close all open HTML pages
Call CloseAllHTMLDocuments()

'Get the path of current directory
FilePath = CreateObject("Scripting.FileSystemObject").GetAbsolutePathName(".")

'Get files to comapare
File1 = FilePath &"\files\File1.xml"
File2 = FilePath &"\files\File2.xml"

'Call ComapareXMLFiles function
Call ComapareXMLFiles(File1,File2)
If ComapareXMLFiles(File1,File2) = True Then
    MsgBox "Files are identical."
Else
    MsgBox "Files are different."
End If

'ComapareXMLFiles function 
Function ComapareXMLFiles(File1,File2)
    Set xmlDoc1 = CreateObject("Msxml2.DOMDocument")
    xmlDoc1.load(File1)
    Set xmlDoc2 = CreateObject("Msxml2.DOMDocument")            
    xmlDoc2.load(File2)     
    Set ElemList1 = xmlDoc1.getElementsByTagName(XMLTAG)
    Set ElemList2 = xmlDoc2.getElementsByTagName(XMLTAG)   
    IF strcomp(ElemList1.item(0).Text,ElemList2.item(0).Text) = 0 then
        Flag = True
    Else
        Flag = False
    End if    
    Set ElemList1 = Nothing
    Set ElemList2 = Nothing   
    Set xmlDoc1 = Nothing
    Set xmlDoc2 = Nothing
    Set xmlDoc = Nothing
ComapareXMLFiles = Flag
End Function

'Close all open HTML pages
Function CloseAllHTMLDocuments()
Dim WshShell, oExec
Set WshShell = CreateObject("WScript.Shell")
Set oExec = WshShell.Exec("taskkill /f /im iexplore.exe")
WScript.sleep 2000
Set WshShell = Nothing
Set oExec = Nothing
End Function

'############################################################################

'Compare PPT files

'Close all open PPT documents
Call CloseAllPPTDocuments()

'Get the path of current directory
FilePath = CreateObject("Scripting.FileSystemObject").GetAbsolutePathName(".")

'Get files to comapare
File1 = FilePath &"\files\File1.pptx"
File2 = FilePath &"\files\File2.pptx"

'Call ComaparePPTFiles function
Call ComaparePPTFiles(File1,File2)
If ComaparePPTFiles(File1,File2) = True Then
    MsgBox "Files are identical."
Else
    MsgBox "Files are different."
End If

'Close all open PPT documents
Call CloseAllPPTDocuments()

'ComaparePPTFiles function
Function ComaparePPTFiles(File1,File2)
    Set objPPT = CreateObject("PowerPoint.Application")   
    objppt.Visible = True   
    Set objppt1 =  objPPT.Presentations.Open(File1, False, True, True)
    set objppt2 =  objPPT.Presentations.Open(File2, False, True, True)
    objPPT.Activate
    Set slide1 = objppt1.Slides(1)
    Set slide2 = objppt2.Slides(1)
    objppt1.Slides(1).Select
    objppt2.Slides(1).Select
    ShapesIndex = slide1.shapes.count  
    For i = 1 to ShapesIndex
    IF strcomp(slide1.Shapes(i).TextFrame.TextRange.Text,slide2.Shapes(i).TextFrame.TextRange.Text) = 0 then
        Flag = True
    Else
        Flag = False
        Exit for
    End if      
    Next           
    objPPT.Quit
    Set slide1 = Nothing
    Set slide2 = Nothing
    Set objppt1 = Nothing
    Set objppt2 = Nothing
    Set objPPT = Nothing
    ComaparePPTFiles = Flag
End Function

'Close all open PPT documents
Function CloseAllPPTDocuments()
Dim WshShell, oExec
Set WshShell = CreateObject("WScript.Shell")
Set oExec = WshShell.Exec("taskkill /f /im POWERPNT.exe")
WScript.sleep 2000
Set WshShell = Nothing
Set oExec = Nothing
End Function

'############################################################################

'Compare RTF and Word files

'Close all open Word documents
Call CloseAllWordDocuments()

'Get the path of current directory
FilePath = CreateObject("Scripting.FileSystemObject").GetAbsolutePathName(".")

'Get files to comapare
File1 = FilePath &"\files\File1.rtf"
File2 = FilePath &"\files\File2.rtf"

'Call ComapareRTFFiles function
Call ComapareRTFFiles(File1,File2)
If ComapareRTFFiles(File1,File2) = True Then
    MsgBox "Files are identical."
Else
    MsgBox "Files are different."
End If

'ComapareRTFFiles function
Function ComapareRTFFiles(File1,File2)
    Set objword = CreateObject("Word.Application")   
    objWord.Visible = False
    Set objDoc1 =  objWord.Documents.Open(File1)
    set objDoc2 =  objWord.Documents.Open(File2)
    Set currentDocument1 = objWord.Documents(1)
    Set currentDocument2 = objWord.Documents(2)
   
    objDoc1 = currentDocument1.words.Count
    objDoc2 = currentDocument1.words.Count
    For i = 1 to objDoc1 - 1
   
        If strcomp(currentDocument1.words(i),currentDocument2.words(i)) = 0 then
            Flag = True         
        Else
            Flag = False    
    Exit for
        End if
    Next
    objword.Quit
    Set currentDocument1 = Nothing
    Set currentDocument2 = Nothing     
    Set objDoc1 = Nothing
    Set objDoc2 = Nothing
    Set objword = Nothing
    Set FilePath = Nothing
ComapareRTFFiles = Flag
End Function   

'Close all open Word documents
Function CloseAllWordDocuments()
Dim WshShell, oExec
Set WshShell = CreateObject("WScript.Shell")
Set oExec = WshShell.Exec("taskkill /f /im WINWORD.exe")
WScript.sleep 2000
Set WshShell = Nothing
Set oExec = Nothing
End Function

Comments

chaturchand said…
Good Job....This is Really Helpful.
Anonymous said…
Really really helpful!
Thank you very much fro such a great job and sharing!

Popular Posts

Demystifying Automation Frameworks: A Comprehensive Guide to Building Scalable Solutions

Mastering Java Collections: Your Secret Weapon for Robust Automation Frameworks

Guide to Database Testing

The Singleton Pattern in Test Automation: Ensuring Consistency and Efficient Resource Management

Key Differences Between Different Programming and Scripting Languages