(view source code of filediff.vbs as plain text)
Option Explicit
Dim arrVer1, arrVer2
Dim blnChkEq, blnChkGt, blnChkLt, blnDebug
Dim blnTime, blnSize, blnVersion
Dim dtmFile1, dtmFile2
Dim intArgs, intDiff, intRC, intSize1, intSize2, intUBound
Dim objFile1, objFile2, objFolder1, objFolder2
Dim objFSO, objShell
Dim strFile1, strFile2, strFolder1, strFolder2
Dim strMsg, strPath1, strPath2, strVer1, strVer2
intArgs = 0
intDiff = 0
intRC = 1
blnChkEq = False
blnChkGt = False
blnChkLt = False
blnDebug = False
blnSize = False
blnTime = False
blnVersion = False
' Create a FileSystemObject instance
Set objFSO = CreateObject( "Scripting.FileSystemObject" )
' Check the command line arguments
With WScript.Arguments
' Check if 2 valid file names were specified
If .Unnamed.Count <> 2 Then Syntax
If Not objFSO.FileExists( .Unnamed(0) ) Then Syntax
If Not objFSO.FileExists( .Unnamed(1) ) Then Syntax
strPath1 = .Unnamed(0)
strPath2 = .Unnamed(1)
' Check the mandatory mutually exclusive switches /S, /T and /V
If .Named.Exists( "S" ) Then
blnSize = True
intArgs = intArgs + 1
End If
If .Named.Exists( "T" ) Then
blnTime = True
intArgs = intArgs + 1
End If
If .Named.Exists( "V" ) Then
blnVersion = True
intArgs = intArgs + 1
End If
If intArgs <> 1 Then Syntax
' Check the optional mutually exclusive switches /E, /G and /L
If .Named.Exists( "E" ) Then
blnChkEq = True
intArgs = intArgs + 1
End If
If .Named.Exists( "G" ) Then
blnChkGt = True
intArgs = intArgs + 1
End If
If .Named.Exists( "L" ) Then
blnChkLt = True
intArgs = intArgs + 1
End If
If intArgs = 1 Then
blnChkEq = True
blnChkGt = True
End If
' Check if invalid switches or combinations were passed
If intArgs > 2 Then Syntax
' Check if we need to display debugging information
If .Named.Exists( "DEBUG" ) Then
blnDebug = True
intArgs = intArgs + 1
strMsg = "Check if Equal = " & blnChkEq & vbCrLf _
& "Check if Greater = " & blnChkGt & vbCrLf _
& "Check if Less = " & blnChkLt & vbCrLf _
& "Debugging Mode = " & blnDebug & vbCrLf _
& "Compare File Sizes = " & blnSize & vbCrLf _
& "Compare Timestamps = " & blnTime & vbCrLf _
& "Compare Versions = " & blnVersion & vbCrLf _
& "File1 Path = " & strPath1 & vbCrLf _
& "File2 Path = " & strPath1
WScript.Echo strMsg
End If
If intArgs <> .Named.Count Then Syntax
End With
With objFSO
If blnVersion Then
strVer1 = objFSO.GetFileVersion( strPath1 )
strVer2 = objFSO.GetFileVersion( strPath2 )
strMsg = "File1 Version = " & strVer1 & vbCrLf _
& "File2 Version = " & strVer1
If blnDebug Then WScript.Echo strMsg
Else
Set objShell = CreateObject( "Shell.Application" )
strFolder1 = .GetParentFolderName( strPath1 )
strFile1 = .GetFileName( strPath1 )
Set objFolder1 = objShell.NameSpace( strFolder1 )
Set objFile1 = objFolder1.ParseName( strFile1 )
strFolder2 = .GetParentFolderName( strPath2 )
strFile2 = .GetFileName( strPath2 )
strMsg = "File1 Folder = " & strFolder1 & vbCrLf _
& "File2 Folder = " & strFolder2 & vbCrLf _
& "File1 Name = " & strFile1 & vbCrLf _
& "File2 Name = " & strFile2
If blnDebug Then WScript.Echo strMsg
Set objFolder2 = objShell.NameSpace( strFolder2 )
Set objFile2 = objFolder1.ParseName( strFile2 )
End If
End With
If blnSize Then
' Compare file sizes
intSize1 = objFile1.Size
intSize2 = objFile2.Size
intDiff = Sgn( intSize1 - intSize2 )
strMsg = "File1 Size = " & intSize1 & vbCrLf _
& "File2 Size = " & intSize2 & vbCrLf _
& "Difference Sign = " & intDiff
If blnDebug Then WScript.Echo strMsg
Set objShell = Nothing
ElseIf blnTime Then
' Compare timestamps
dtmFile1 = objFile1.ModifyDate
dtmFile2 = objFile2.ModifyDate
intDiff = Sgn( DateDiff( "s", dtmFile2, dtmFile1 ) )
strMsg = "File1 Timestamp = " & dtmFile1 & vbCrLf _
& "File2 Timestamp = " & dtmFile2 & vbCrLf _
& "Difference Sign = " & intDiff
If blnDebug Then WScript.Echo strMsg
Set objShell = Nothing
Else
' Compare file versions
intDiff = 0
If strVer1 <> strVer2 Then
arrVer1 = Split( Replace( strVer1, ",", "." ), "." )
arrVer2 = Split( Replace( strVer2, ",", "." ), "." )
intUBound = UBound( arrVer1 )
If UBound( arrVer1 ) <> UBound( arrVer2 ) Then
If UBound( arrVer1 ) > UBound( arrVer2 ) Then
For i = 0 To UBound( arrVer2 )
If arrVer1(i) > arrVer2(i) Then intDiff = 1
If arrVer1(i) < arrVer2(i) Then intDiff = -1
If arrVer1(i) <> arrVer2(i) Then Exit For
Next
For i = UBound( arrVer2 ) + 1 To UBound( arrVer1 )
If arrVer1(i) > 0 Then intDiff = 1
Next
Else
For i = 0 To UBound( arrVer1 )
If arrVer1(i) > arrVer2(i) Then intDiff = 1
If arrVer1(i) < arrVer2(i) Then intDiff = -1
If arrVer1(i) <> arrVer2(i) Then Exit For
Next
For i = UBound( arrVer1 ) + 1 To UBound( arrVer2 )
If arrVer2(i) > 0 Then intDiff = -1
Next
End If
End If
End If
If blnDebug Then WScript.Echo "Difference Sign = " & intDiff
End If
Set objFSO = Nothing
Select Case intDiff
Case -1
If blnChkLt Then intRC = 0
Case 0
If blnChkEq Then intRC = 0
Case 1
If blnChkGt Then intRC = 0
Case Else
Syntax
End Select
If blnDebug Then WScript.Echo "Return Code = " & intRC
WScript.Quit intRC
Sub Syntax( )
strMsg = "FileDiff.vbs, Version 1.00" & vbCrLf _
& "Check if two files differ in size, timestamp or version" _
& vbCrLf & vbCrLf _
& "Usage: CSCRIPT FILEDIFF.VBS file1 file2 property [ comparison ] [ /DEBUG ]" _
& vbCrLf & vbCrLf _
& "Where: file1, file2 are the files to be compared" _
& vbCrLf _
& " ""property"" (mandatory) can be one of the following:" _
& vbCrLf _
& " /S compare file sizes" _
& vbCrLf _
& " /T compare timestamps" _
& vbCrLf _
& " /V compare file versions" _
& vbCrLf _
& " ""comparison"" (optional) can be one of the following:" _
& vbCrLf _
& " /L check if file1's value is less than file2's" _
& vbCrLf _
& " /E check if values are equal" _
& vbCrLf _
& " /G check if file1's value is greater than file2's" _
& vbCrLf _
& " (default: check if file1's value is greater or equal)" _
& vbCrLf _
& " /DEBUG display verbose debugging information" _
& vbCrLf & vbCrLf _
& "Return: 0 if comparison returns true, 1 if false" _
& vbCrLf & vbCrLf _
& "Examples: Return code:" _
& vbCrLf _
& "FILEDIFF.VBS file1 file2 /T /L 0 if file1 is older than file2, otherwise 1" _
& vbCrLf _
& "FILEDIFF.VBS file1 file2 /S /E 0 if files are of equal size, 1 if not" _
& vbCrLf & vbCrLf _
& "Written by Rob van der Woude" & vbCrLf _
& "http://www.robvanderwoude.com"
WScript.Echo strMsg
WScript.Quit 2
End Sub
page last modified: 2024-04-16; loaded in 0.0114 seconds