using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Security.Cryptography; namespace RobvanderWoude { internal class CompareFiles { static string progver = "1.01"; static int Main( string[] args ) { #region Parse Command Line if ( args.Length < 2 || args.Length > 3 || args.Contains( "/?" ) ) { return ShowHelp( ); } string file1 = args[0]; if ( !File.Exists( file1 ) ) { ShowHelp( "File not found: \"{0}\"", file1 ); return -2; } string file2 = args[1]; if ( !File.Exists( file2 ) ) { ShowHelp( "File not found: \"{0}\"", file2 ); return -3; } bool ignoretimestamps = false; if ( args.Length == 3 ) { if ( args[2].ToUpper( ) == "/IT" ) { ignoretimestamps = true; } else { return ShowHelp( "Invalid command line argument \"{0}\"", args[2] ); } } #endregion Parse Command Line #region Compare File Sizes long file1size = new FileInfo( file1 ).Length; long file2size = new FileInfo( file2 ).Length; if ( file1size > file2size ) { Console.WriteLine( "\"{0}\" is bigger", file1 ); return 1; } else if ( file1size < file2size ) { Console.WriteLine( "\"{0}\" is bigger", file2 ); return 2; } else { Console.WriteLine( "File sizes match" ); } #endregion Compare File Sizes #region Compare Timestamps DateTime file1timestamp = File.GetLastWriteTime( file1 ); DateTime file2timestamp = File.GetLastWriteTime( file2 ); if ( file1timestamp > file2timestamp ) { Console.WriteLine( "\"{0}\" is newer", file1 ); if ( !ignoretimestamps ) { return 3; } } else if ( file1timestamp < file2timestamp ) { Console.WriteLine( "\"{0}\" is newer", file2 ); if ( !ignoretimestamps ) { return 4; } } else { Console.WriteLine( "Timestamps match" ); } #endregion Compare Timestamps #region Compare MD5 Checksums string file1md5; string file2md5; using ( var md5 = MD5.Create( ) ) { using ( var stream = File.OpenRead( file1 ) ) { file1md5 = BitConverter.ToString( md5.ComputeHash( stream ) ).Replace( "-", "" ).ToLowerInvariant( ); } using ( var stream = File.OpenRead( file2 ) ) { file2md5 = BitConverter.ToString( md5.ComputeHash( stream ) ).Replace( "-", "" ).ToLowerInvariant( ); } } if ( file1md5 == file2md5 ) { Console.WriteLine( "MD5 checksums match" ); } else { Console.WriteLine( "Files have different MD5 checksums" ); return 5; } #endregion Compare MD5 Checksums Console.WriteLine( "Files are identical" ); return 0; } #region Error handling static int ShowHelp( params string[] errmsg ) { #region Error Message if ( errmsg.Length > 0 ) { List errargs = new List( errmsg ); errargs.RemoveAt( 0 ); Console.Error.WriteLine( ); Console.ForegroundColor = ConsoleColor.Red; Console.Error.Write( "ERROR:\t" ); Console.ForegroundColor = ConsoleColor.White; Console.Error.WriteLine( errmsg[0], errargs.ToArray( ) ); Console.ResetColor( ); } #endregion Error Message #region Help Text /* CompareFiles.exe, Version 1.00 Check if two files are identical Usage: CompareFiles.exe file1 file2 [ /IT ] Where: file1 and file2 are the files to compare /IT Ignore Timestamps (continue even if timestamps differ) Return code: 0 files are identical 1 file1 is bigger 2 file1 is smaller 3 sizes match but file1 is newer 4 sizes match but file1 is older 5 MD5 checksums are different -1 command line error -2 file1 not found -3 file2 not found Written by Rob van der Woude https://www.robvanderwoude.com */ #endregion Help Text #region Display Help Text Console.Error.WriteLine( ); Console.Error.WriteLine( "CompareFiles.exe, Version {0}", progver ); Console.Error.WriteLine( "Check if two files are identical" ); Console.Error.WriteLine( ); Console.Error.Write( "Usage: " ); Console.ForegroundColor = ConsoleColor.White; Console.Error.WriteLine( "CompareFiles.exe file1 file2 [ /IT ]" ); Console.ResetColor( ); Console.Error.WriteLine( ); Console.Error.Write( "Where: " ); Console.ForegroundColor = ConsoleColor.White; Console.Error.Write( "file1" ); Console.ResetColor( ); Console.Error.Write( " and " ); Console.ForegroundColor = ConsoleColor.White; Console.Error.Write( "file2" ); Console.ResetColor( ); Console.Error.WriteLine( " are the files to compare" ); Console.ForegroundColor = ConsoleColor.White; Console.Error.Write( " /IT I" ); Console.ResetColor( ); Console.Error.Write( "gnore " ); Console.ForegroundColor = ConsoleColor.White; Console.Error.Write( "T" ); Console.ResetColor( ); Console.Error.WriteLine( "imestamps (continue even if timestamps differ)" ); Console.Error.WriteLine( ); Console.Error.WriteLine( "Return code: 0 files are identical" ); Console.Error.WriteLine( " 1 file1 is bigger" ); Console.Error.WriteLine( " 2 file1 is smaller" ); Console.Error.WriteLine( " 3 sizes match but file1 is newer" ); Console.Error.WriteLine( " 4 sizes match but file1 is older" ); Console.Error.WriteLine( " 5 MD5 checksums are different" ); Console.Error.WriteLine( " -1 command line error" ); Console.Error.WriteLine( " -2 file1 not found" ); Console.Error.WriteLine( " -3 file2 not found" ); Console.Error.WriteLine( ); Console.Error.WriteLine( "Written by Rob van der Woude" ); Console.Error.WriteLine( "https://www.robvanderwoude.com" ); #endregion Display Help Text return -1; } #endregion Error handling } }