Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for filediff.vbs

(view source code of filediff.vbs as plain text)

  1. Option Explicit
  2.  
  3. Dim arrVer1, arrVer2
  4. Dim blnChkEq, blnChkGt, blnChkLt, blnDebug
  5. Dim blnTime, blnSize, blnVersion
  6. Dim dtmFile1, dtmFile2
  7. Dim intArgs, intDiff, intRC, intSize1, intSize2, intUBound
  8. Dim objFile1, objFile2, objFolder1, objFolder2
  9. Dim objFSO, objShell
  10. Dim strFile1, strFile2, strFolder1, strFolder2
  11. Dim strMsg, strPath1, strPath2, strVer1, strVer2
  12.  
  13. intArgs    = 0
  14. intDiff    = 0
  15. intRC      = 1
  16. blnChkEq   = False
  17. blnChkGt   = False
  18. blnChkLt   = False
  19. blnDebug   = False
  20. blnSize    = False
  21. blnTime    = False
  22. blnVersion = False
  23.  
  24. ' Create a FileSystemObject instance
  25. Set objFSO = CreateObject( "Scripting.FileSystemObject" )
  26.  
  27. ' Check the command line arguments
  28. With WScript.Arguments
  29. 	' Check if 2 valid file names were specified
  30. 	If .Unnamed.Count <> 2 Then Syntax
  31. 	If Not objFSO.FileExists( .Unnamed(0) ) Then Syntax
  32. 	If Not objFSO.FileExists( .Unnamed(1) ) Then Syntax
  33. 	strPath1 = .Unnamed(0)
  34. 	strPath2 = .Unnamed(1)
  35. 	' Check the mandatory mutually exclusive switches /S, /T and /V
  36. 	If .Named.Exists( "S" ) Then
  37. 		blnSize = True
  38. 		intArgs = intArgs + 1
  39. 	End If
  40. 	If .Named.Exists( "T" ) Then
  41. 		blnTime = True
  42. 		intArgs = intArgs + 1
  43. 	End If
  44. 	If .Named.Exists( "V" ) Then
  45. 		blnVersion = True
  46. 		intArgs    = intArgs + 1
  47. 	End If
  48. 	If intArgs <> 1 Then Syntax
  49. 	' Check the optional mutually exclusive switches /E, /G and /L
  50. 	If .Named.Exists( "E" ) Then
  51. 		blnChkEq = True
  52. 		intArgs  = intArgs + 1
  53. 	End If
  54. 	If .Named.Exists( "G" ) Then
  55. 		blnChkGt = True
  56. 		intArgs  = intArgs + 1
  57. 	End If
  58. 	If .Named.Exists( "L" ) Then
  59. 		blnChkLt = True
  60. 		intArgs  = intArgs + 1
  61. 	End If
  62. 	If intArgs = 1 Then
  63. 		blnChkEq = True
  64. 		blnChkGt = True
  65. 	End If
  66. 	' Check if invalid switches or combinations were passed
  67. 	If intArgs > 2 Then Syntax
  68. 	' Check if we need to display debugging information
  69. 	If .Named.Exists( "DEBUG" ) Then
  70. 		blnDebug = True
  71. 		intArgs  = intArgs + 1
  72. 		strMsg   = "Check if Equal     = " & blnChkEq   & vbCrLf _
  73. 		         & "Check if Greater   = " & blnChkGt   & vbCrLf _
  74. 		         & "Check if Less      = " & blnChkLt   & vbCrLf _
  75. 		         & "Debugging Mode     = " & blnDebug   & vbCrLf _
  76. 		         & "Compare File Sizes = " & blnSize    & vbCrLf _
  77. 		         & "Compare Timestamps = " & blnTime    & vbCrLf _
  78. 		         & "Compare Versions   = " & blnVersion & vbCrLf _
  79. 		         & "File1 Path         = " & strPath1   & vbCrLf _
  80. 		         & "File2 Path         = " & strPath1
  81. 		WScript.Echo strMsg
  82. 	End If
  83. 	If intArgs <> .Named.Count Then Syntax
  84. End With
  85.  
  86. With objFSO
  87. 	If blnVersion Then
  88. 		strVer1 = objFSO.GetFileVersion( strPath1 )
  89. 		strVer2 = objFSO.GetFileVersion( strPath2 )
  90. 		strMsg  = "File1 Version      = " & strVer1 & vbCrLf _
  91. 		        & "File2 Version      = " & strVer1
  92. 		If blnDebug Then WScript.Echo strMsg
  93. 	Else
  94. 		Set objShell   = CreateObject( "Shell.Application" )
  95. 		strFolder1 = .GetParentFolderName( strPath1 )
  96. 		strFile1   = .GetFileName( strPath1 )
  97. 		Set objFolder1 = objShell.NameSpace( strFolder1 )
  98. 		Set objFile1   = objFolder1.ParseName( strFile1 )
  99. 		strFolder2 = .GetParentFolderName( strPath2 )
  100. 		strFile2   = .GetFileName( strPath2 )
  101. 		strMsg  = "File1 Folder       = " & strFolder1 & vbCrLf _
  102. 		        & "File2 Folder       = " & strFolder2 & vbCrLf _
  103. 		        & "File1 Name         = " & strFile1   & vbCrLf _
  104. 		        & "File2 Name         = " & strFile2
  105. 		If blnDebug Then WScript.Echo strMsg
  106. 		Set objFolder2 = objShell.NameSpace( strFolder2 )
  107. 		Set objFile2   = objFolder1.ParseName( strFile2 )
  108. 	End If
  109. End With
  110.  
  111. If blnSize Then
  112. 	' Compare file sizes
  113. 	intSize1 = objFile1.Size
  114. 	intSize2 = objFile2.Size
  115. 	intDiff = Sgn( intSize1 - intSize2 )
  116. 	strMsg   = "File1 Size         = " & intSize1 & vbCrLf _
  117. 	         & "File2 Size         = " & intSize2 & vbCrLf _
  118. 	         & "Difference Sign    = " & intDiff
  119. 	If blnDebug Then WScript.Echo strMsg
  120. 	Set objShell = Nothing
  121. ElseIf blnTime Then
  122. 	' Compare timestamps
  123. 	dtmFile1 = objFile1.ModifyDate
  124. 	dtmFile2 = objFile2.ModifyDate
  125. 	intDiff  = Sgn( DateDiff( "s", dtmFile2, dtmFile1 ) )
  126. 	strMsg   = "File1 Timestamp    = " & dtmFile1 & vbCrLf _
  127. 	         & "File2 Timestamp    = " & dtmFile2 & vbCrLf _
  128. 	         & "Difference Sign    = " & intDiff
  129. 	If blnDebug Then WScript.Echo strMsg
  130. 	Set objShell = Nothing
  131. Else
  132. 	' Compare file versions
  133. 	intDiff = 0
  134. 	If strVer1 <> strVer2 Then
  135. 		arrVer1 = Split( Replace( strVer1, ",", "." ), "." )
  136. 		arrVer2 = Split( Replace( strVer2, ",", "." ), "." )
  137. 		intUBound = UBound( arrVer1 )
  138. 		If UBound( arrVer1 ) <> UBound( arrVer2 ) Then
  139. 			If UBound( arrVer1 ) > UBound( arrVer2 ) Then
  140. 				For i = 0 To UBound( arrVer2 )
  141. 					If arrVer1(i) >  arrVer2(i) Then intDiff =  1
  142. 					If arrVer1(i) <  arrVer2(i) Then intDiff = -1
  143. 					If arrVer1(i) <> arrVer2(i) Then Exit For
  144. 				Next
  145. 				For i = UBound( arrVer2 ) + 1 To UBound( arrVer1 )
  146. 					If arrVer1(i) > 0 Then intDiff = 1
  147. 				Next
  148. 			Else
  149. 				For i = 0 To UBound( arrVer1 )
  150. 					If arrVer1(i) >  arrVer2(i) Then intDiff =  1
  151. 					If arrVer1(i) <  arrVer2(i) Then intDiff = -1
  152. 					If arrVer1(i) <> arrVer2(i) Then Exit For
  153. 				Next
  154. 				For i = UBound( arrVer1 ) + 1 To UBound( arrVer2 )
  155. 					If arrVer2(i) > 0 Then intDiff = -1
  156. 				Next
  157. 			End If
  158. 		End If
  159. 	End If
  160. 	If blnDebug Then WScript.Echo "Difference Sign    = " & intDiff
  161. End If
  162.  
  163. Set objFSO = Nothing
  164.  
  165. Select Case intDiff
  166. 	Case -1
  167. 		If blnChkLt Then intRC = 0
  168. 	Case 0
  169. 		If blnChkEq Then intRC = 0
  170. 	Case 1
  171. 		If blnChkGt Then intRC = 0
  172. 	Case Else
  173. 		Syntax
  174. End Select
  175.  
  176. If blnDebug Then WScript.Echo "Return Code        = " & intRC
  177. WScript.Quit intRC
  178.  
  179.  
  180. Sub Syntax( )
  181. 	strMsg = "FileDiff.vbs,  Version 1.00" & vbCrLf _
  182. 	       & "Check if two files differ in size, timestamp or version" _
  183. 	       & vbCrLf & vbCrLf _
  184. 	       & "Usage:   CSCRIPT FILEDIFF.VBS file1 file2 property [ comparison ] [ /DEBUG ]" _
  185. 	       & vbCrLf & vbCrLf _
  186. 	       & "Where:   file1, file2  are the files to be compared" _
  187. 	       & vbCrLf _
  188. 	       & "         ""property""    (mandatory) can be one of the following:" _
  189. 	       & vbCrLf _
  190. 	       & "                 /S    compare file sizes" _
  191. 	       & vbCrLf _
  192. 	       & "                 /T    compare timestamps" _
  193. 	       & vbCrLf _
  194. 	       & "                 /V    compare file versions" _
  195. 	       & vbCrLf _
  196. 	       & "         ""comparison""  (optional) can be one of the following:" _
  197. 	       & vbCrLf _
  198. 	       & "                 /L    check if file1's value is less than file2's" _
  199. 	       & vbCrLf _
  200. 	       & "                 /E    check if values are equal" _
  201. 	       & vbCrLf _
  202. 	       & "                 /G    check if file1's value is greater than file2's" _
  203. 	       & vbCrLf _
  204. 	       & "                       (default: check if file1's value is greater or equal)" _
  205. 	       & vbCrLf _
  206. 	       & "         /DEBUG        display verbose debugging information" _
  207. 	       & vbCrLf & vbCrLf _
  208. 	       & "Return:  0 if comparison returns true, 1 if false" _
  209. 	       & vbCrLf & vbCrLf _
  210. 	       & "Examples:                           Return code:" _
  211. 	       & vbCrLf _
  212. 	       & "FILEDIFF.VBS file1 file2 /T /L      0 if file1 is older than file2, otherwise 1" _
  213. 	       & vbCrLf _
  214. 	       & "FILEDIFF.VBS file1 file2 /S /E      0 if files are of equal size, 1 if not" _
  215. 	       & vbCrLf & vbCrLf _
  216. 	       & "Written by Rob van der Woude" & vbCrLf _
  217. 	       & "http://www.robvanderwoude.com"
  218. 	WScript.Echo strMsg
  219. 	WScript.Quit 2
  220. End Sub

page last modified: 2024-02-26; loaded in 0.0222 seconds