Execute WinMerge via Excel VBA Tool

Execute WinMerge via Excel VBA Tool

Summary

The Excel VBA tool to execute WinMerge.
Via the tool, WinMerge compare files in the specified folder including those in subfolders, and output the comparison results in HTML format.

Files with the same name in the same folder structure are targeted.

How to use

Input Setting Values in the ‘Tool’ sheet in the tool.
Push Button in the ‘Tool’ sheet in the tool.
WinMerge exports the comparison results in HTML format.

Setting

Source code

Option Explicit

‘********************************************************************
‘[Summary]
‘ Via this tool, WinMerge compare files in the specified folder
‘ including those in subfolders, and output the comparison results
‘ in HTML format.
‘ Files with the same name in the same folder structure are targeted.
‘********************************************************************

‘Declaration FileSystemObject as module-level variable.
Dim FSO As Object

‘*******************************************************************
‘[Summary]
‘ Main Procedure
‘*******************************************************************
Sub ExportWinMergeReport()

Dim inputFolderPath1 As String ‘first comparison target folder
Dim inputFolderPath2 As String ‘secound comparison target folder
Dim outputFolderPath As String ‘destination folder for the reports of WinMerge
Dim filePathWinMerge As String ‘file path of the executable program of WinMerge
Dim startTime As Date ‘Start Time

‘Start Time
startTime = Now

‘Assign Setting Value Into Variables
With ThisWorkbook.Sheets(“Tool”)
inputFolderPath1 = .Range(“B2”).Value
inputFolderPath2 = .Range(“B3”).Value
outputFolderPath = .Range(“B4”).Value
filePathWinMerge = .Range(“B5”).Value
End With

‘Input Check
If inputFolderPath1 = “” Then
MsgBox “The first comparison target folder is not entered in the input field.”, vbInformation
Exit Sub
End If

If inputFolderPath2 = “” Then
MsgBox “The secound comparison target folder is not entered in the input field.”, vbInformation
Exit Sub
End If

If outputFolderPath = “” Then
MsgBox “The output Folder is not entered in the input field.”, vbInformation
Exit Sub
End If

If filePathWinMerge = “” Then
MsgBox “The file path of the executable program of WinMerge is not entered.”, vbInformation
Exit Sub
End If

‘Check if folders or files exist
Set FSO = CreateObject(“Scripting.FileSystemObject”)

If Not FSO.FolderExists(inputFolderPath1) Then
MsgBox “Folder1 to compare doesn’t exit.”, vbInformation
Set FSO = Nothing
Exit Sub
End If

If Not FSO.FolderExists(inputFolderPath2) Then
MsgBox “Folder2 to compare doesn’t exit.”, vbInformation
Set FSO = Nothing
Exit Sub
End If

If Not FSO.FileExists(filePathWinMerge) Then
MsgBox “Exe file of WinMerge doesn’t exist”, vbInformation
Set FSO = Nothing
Exit Sub
End If

‘Execute WinMerge
Call execWinMerge(inputFolderPath1, inputFolderPath2, outputFolderPath, filePathWinMerge)

Set FSO = Nothing

‘Complete Message
MsgBox “Complete.” & vbLf & “Time “ & Format(Now startTime, “hh:mm:ss”), vbInformation

End Sub

‘*******************************************************************
‘[Summary]
‘ Sub procedure to execute WinMerge

‘[Argument]
‘ inputFolderPath1 : first comparison target folder
‘ inputFolderPath2 : secound comparison target folder
‘ outputFolderPath : destination folder for the reports of WinMerge
‘ filePathWinMerge : file path of the executable program of WinMerge
‘*******************************************************************
Sub execWinMerge(ByVal inputFolderPath1 As String, _
ByVal inputFolderPath2 As String, _
ByVal outputFolderPath As String, _
ByVal filePathWinMerge As String)

Dim inputFilePath1 As String
Dim inputFilePath2 As String
Dim outputFilePath As String
Dim subFolderPath As String
Dim exeCode As String
Dim oFile As Object
Dim subfolder As Object
Dim exeStartTime As Date
Dim canContinue As Boolean
Dim exeResult As Long

‘Make the folder to export results of WinMerge
If Not FSO.FolderExists(outputFolderPath) Then
FSO.CreateFolder (outputFolderPath)
End If

‘Execute WinMerge
For Each oFile In FSO.GetFolder(inputFolderPath1).Files

‘Check for the existence of the comparison target file, and if the same file exists, execute WinMerge.
If FSO.FileExists(inputFolderPath2 & “” & oFile.Name) Then

inputFilePath1 = inputFolderPath1 & “” & oFile.Name
inputFilePath2 = inputFolderPath2 & “” & oFile.Name
outputFilePath = outputFolderPath & “” & FSO.GetBaseName(oFile) & “.htm”

exeCode = filePathWinMerge & ” “ & inputFilePath1 & ” “ & inputFilePath2 & ” /or “ & outputFilePath & ” /noninteractive /minimize /xq /e /u”
exeResult = Shell(exeCode, vbMinimizedNoFocus)

If exeResult = 0 Then
MsgBox “Failed to execute WinMerge.”, vbInformation
Set FSO = Nothing
End
End If

‘Wait for the output of the comparison result.Maximum time is 10 secounds.
exeStartTime = Now
canContinue = False

Do
If FSO.FileExists(outputFilePath) Then
canContinue = True
End If
Loop Until canContinue Or Now > exeStartTime + TimeSerial(0, 0, 10)

If Not canContinue Then
MsgBox “Failed to output the report of WinMerge.” & vbLf & vbLf & outputFilePath, vbInformation
Set FSO = Nothing
End
End If

End If

Next

‘Execute WinMerge for subfolders as well.
For Each subfolder In FSO.GetFolder(inputFolderPath1).Subfolders

‘If subfolders exist, execute recursive processing.
If subfolder.Name <> “” Then
subFolderPath = subfolder.Path
inputFolderPath2 = inputFolderPath2 & “” & subfolder.Name
outputFolderPath = outputFolderPath & “” & subfolder.Name
Call execWinMerge(subFolderPath, inputFolderPath2, outputFolderPath, filePathWinMerge)
End If

Next

End Sub

Please follow and like us:
Pin Share