Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for comparefiles.cs

(view source code of comparefiles.cs as plain text)

  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Security.Cryptography;
  6.  
  7.  
  8. namespace RobvanderWoude
  9. {
  10. 	internal class CompareFiles
  11. 	{
  12. 		static string progver = "1.01";
  13.  
  14.  
  15. 		static int Main( string[] args )
  16. 		{
  17. 			#region Parse Command Line
  18.  
  19. 			if ( args.Length < 2 || args.Length > 3 || args.Contains( "/?" ) )
  20. 			{
  21. 				return ShowHelp( );
  22. 			}
  23.  
  24. 			string file1 = args[0];
  25. 			if ( !File.Exists( file1 ) )
  26. 			{
  27. 				ShowHelp( "File not found: \"{0}\"", file1 );
  28. 				return -2;
  29. 			}
  30. 			string file2 = args[1];
  31. 			if ( !File.Exists( file2 ) )
  32. 			{
  33. 				ShowHelp( "File not found: \"{0}\"", file2 );
  34. 				return -3;
  35. 			}
  36.  
  37. 			bool ignoretimestamps = false;
  38. 			if ( args.Length == 3 )
  39. 			{
  40. 				if ( args[2].ToUpper( ) == "/IT" )
  41. 				{
  42. 					ignoretimestamps = true;
  43. 				}
  44. 				else
  45. 				{
  46. 					return ShowHelp( "Invalid command line argument \"{0}\"", args[2] );
  47. 				}
  48. 			}
  49.  
  50. 			#endregion Parse Command Line
  51.  
  52.  
  53. 			#region Compare File Sizes
  54.  
  55. 			long file1size = new FileInfo( file1 ).Length;
  56. 			long file2size = new FileInfo( file2 ).Length;
  57. 			if ( file1size > file2size )
  58. 			{
  59. 				Console.WriteLine( "\"{0}\" is bigger", file1 );
  60. 				return 1;
  61. 			}
  62. 			else if ( file1size < file2size )
  63. 			{
  64. 				Console.WriteLine( "\"{0}\" is bigger", file2 );
  65. 				return 2;
  66. 			}
  67. 			else
  68. 			{
  69. 				Console.WriteLine( "File sizes match" );
  70. 			}
  71.  
  72. 			#endregion Compare File Sizes
  73.  
  74.  
  75. 			#region Compare Timestamps
  76.  
  77. 			DateTime file1timestamp = File.GetLastWriteTime( file1 );
  78. 			DateTime file2timestamp = File.GetLastWriteTime( file2 );
  79. 			if ( file1timestamp > file2timestamp )
  80. 			{
  81. 				Console.WriteLine( "\"{0}\" is newer", file1 );
  82. 				if ( !ignoretimestamps )
  83. 				{
  84. 					return 3;
  85. 				}
  86. 			}
  87. 			else if ( file1timestamp < file2timestamp )
  88. 			{
  89. 				Console.WriteLine( "\"{0}\" is newer", file2 );
  90. 				if ( !ignoretimestamps )
  91. 				{
  92. 					return 4;
  93. 				}
  94. 			}
  95. 			else
  96. 			{
  97. 				Console.WriteLine( "Timestamps match" );
  98. 			}
  99.  
  100. 			#endregion Compare Timestamps
  101.  
  102.  
  103. 			#region Compare MD5 Checksums
  104.  
  105. 			string file1md5;
  106. 			string file2md5;
  107. 			using ( var md5 = MD5.Create( ) )
  108. 			{
  109. 				using ( var stream = File.OpenRead( file1 ) )
  110. 				{
  111. 					file1md5 = BitConverter.ToString( md5.ComputeHash( stream ) ).Replace( "-", "" ).ToLowerInvariant( );
  112. 				}
  113. 				using ( var stream = File.OpenRead( file2 ) )
  114. 				{
  115. 					file2md5 = BitConverter.ToString( md5.ComputeHash( stream ) ).Replace( "-", "" ).ToLowerInvariant( );
  116. 				}
  117. 			}
  118. 			if ( file1md5 == file2md5 )
  119. 			{
  120. 				Console.WriteLine( "MD5 checksums match" );
  121. 			}
  122. 			else
  123. 			{
  124. 				Console.WriteLine( "Files have different MD5 checksums" );
  125. 				return 5;
  126. 			}
  127.  
  128. 			#endregion Compare MD5 Checksums
  129.  
  130.  
  131. 			Console.WriteLine( "Files are identical" );
  132. 			return 0;
  133. 		}
  134.  
  135.  
  136. 		#region Error handling
  137.  
  138. 		static int ShowHelp( params string[] errmsg )
  139. 		{
  140. 			#region Error Message
  141.  
  142. 			if ( errmsg.Length > 0 )
  143. 			{
  144. 				List<string> errargs = new List<string>( errmsg );
  145. 				errargs.RemoveAt( 0 );
  146. 				Console.Error.WriteLine( );
  147. 				Console.ForegroundColor = ConsoleColor.Red;
  148. 				Console.Error.Write( "ERROR:\t" );
  149. 				Console.ForegroundColor = ConsoleColor.White;
  150. 				Console.Error.WriteLine( errmsg[0], errargs.ToArray( ) );
  151. 				Console.ResetColor( );
  152. 			}
  153.  
  154. 			#endregion Error Message
  155.  
  156.  
  157. 			#region Help Text
  158.  
  159. 			/*
  160. 			CompareFiles.exe,  Version 1.00
  161. 			Check if two files are identical
  162.  
  163. 			Usage:        CompareFiles.exe  file1  file2  [ /IT ]
  164.  
  165. 			Where:        file1 and file2 are the files to compare
  166. 			              /IT  Ignore Timestamps (continue even if timestamps differ)
  167.  
  168. 			Return code:  0    files are identical
  169. 			              1    file1 is bigger
  170. 			              2    file1 is smaller
  171. 			              3    sizes match but file1 is newer
  172. 			              4    sizes match but file1 is older
  173. 			              5    MD5 checksums are different
  174. 			             -1    command line error
  175. 			             -2    file1 not found
  176. 			             -3    file2 not found
  177.  
  178. 			Written by Rob van der Woude
  179. 			https://www.robvanderwoude.com
  180. 			*/
  181.  
  182. 			#endregion Help Text
  183.  
  184.  
  185. 			#region Display Help Text
  186.  
  187. 			Console.Error.WriteLine( );
  188.  
  189. 			Console.Error.WriteLine( "CompareFiles.exe,  Version {0}", progver );
  190.  
  191. 			Console.Error.WriteLine( "Check if two files are identical" );
  192.  
  193. 			Console.Error.WriteLine( );
  194.  
  195. 			Console.Error.Write( "Usage:        " );
  196. 			Console.ForegroundColor = ConsoleColor.White;
  197. 			Console.Error.WriteLine( "CompareFiles.exe  file1  file2  [ /IT ]" );
  198. 			Console.ResetColor( );
  199.  
  200. 			Console.Error.WriteLine( );
  201.  
  202. 			Console.Error.Write( "Where:        " );
  203. 			Console.ForegroundColor = ConsoleColor.White;
  204. 			Console.Error.Write( "file1" );
  205. 			Console.ResetColor( );
  206. 			Console.Error.Write( " and " );
  207. 			Console.ForegroundColor = ConsoleColor.White;
  208. 			Console.Error.Write( "file2" );
  209. 			Console.ResetColor( );
  210. 			Console.Error.WriteLine( " are the files to compare" );
  211.  
  212. 			Console.ForegroundColor = ConsoleColor.White;
  213. 			Console.Error.Write( "              /IT  I" );
  214. 			Console.ResetColor( );
  215. 			Console.Error.Write( "gnore " );
  216. 			Console.ForegroundColor = ConsoleColor.White;
  217. 			Console.Error.Write( "T" );
  218. 			Console.ResetColor( );
  219. 			Console.Error.WriteLine( "imestamps (continue even if timestamps differ)" );
  220.  
  221. 			Console.Error.WriteLine( );
  222.  
  223.  
  224. 			Console.Error.WriteLine( "Return code:  0   files are identical" );
  225.  
  226. 			Console.Error.WriteLine( "              1   file1 is bigger" );
  227.  
  228. 			Console.Error.WriteLine( "              2   file1 is smaller" );
  229.  
  230. 			Console.Error.WriteLine( "              3   sizes match but file1 is newer" );
  231.  
  232. 			Console.Error.WriteLine( "              4   sizes match but file1 is older" );
  233.  
  234. 			Console.Error.WriteLine( "              5   MD5 checksums are different" );
  235.  
  236. 			Console.Error.WriteLine( "             -1   command line error" );
  237.  
  238. 			Console.Error.WriteLine( "             -2   file1 not found" );
  239.  
  240. 			Console.Error.WriteLine( "             -3   file2 not found" );
  241.  
  242. 			Console.Error.WriteLine( );
  243.  
  244. 			Console.Error.WriteLine( "Written by Rob van der Woude" );
  245.  
  246. 			Console.Error.WriteLine( "https://www.robvanderwoude.com" );
  247.  
  248. 			#endregion Display Help Text
  249.  
  250.  
  251. 			return -1;
  252. 		}
  253.  
  254. 		#endregion Error handling
  255. 	}
  256. }
  257.  

page last modified: 2024-04-16; loaded in 0.0079 seconds