Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
Private Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As Long, lpExitCode As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Const SYNCHRONIZE = &H100000
Const PROCESS_QUERY_INFORMATION = &H400&
Const INFINITE = -1&
'--- Shells the passed command line and waits for the process to finish
'--- Returns the exit code of the shelled process
Public Function ShellWait(PathName As String, Optional ShowCommand As VbAppWinStyle = vbNormalFocus) As Long
Dim ProcessId As Long, hProcess As Long
'run the process and get its process id
ProcessId = Shell(PathName, ShowCommand)
'open the process handle
hProcess = OpenProcess(PROCESS_QUERY_INFORMATION Or SYNCHRONIZE, 0, ProcessId)
'wait until the process terminates
WaitForSingleObject hProcess, INFINITE
'get the exit code
GetExitCodeProcess hProcess, ShellWait
'close the proces handle
CloseHandle hProcess
End Function
Private Function CompareFiles(ByVal File1 As String, ByVal File2 As String) As Boolean
' This function returns TRUE of the files are the same
Dim OutputFile As String
Dim ShellCommand As String
Dim FSO As Scripting.FileSystemObject
Dim strTemp As String
OutputFile = App.Path & "\Compare.txt"
CompareFiles = False
Set FSO = CreateObject("Scripting.FileSystemObject")
If FSO.GetFile(File1).Size = FSO.GetFile(File2).Size Then
If FSO.FileExists(OutputFile) Then
Call FSO.DeleteFile(OutputFile)
End If
ShellCommand = "CMD /c FC """ & File1 & """ """ & File2 & """ > """ & OutputFile & """"
Call ShellWait(ShellCommand, vbNormalFocus)
strTemp = FSO.OpenTextFile(OutputFile, ForReading).ReadAll
If InStr(strTemp, "FC: no differences encountered") > 0 Then
CompareFiles = True
End If
End If
End Function
Private Sub Command1_Click()
If CompareFiles("C:\Path\File.ext", "C:\OtherPath\File.ext") Then
MsgBox "Files are the same"
Else
MsgBox "Files are different"
End If
End Sub