Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for clonedate.cs

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

  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5.  
  6. namespace RobvanderWoude
  7. {
  8. 	class CloneDate
  9. 	{
  10. 		static string progver = "1.00";
  11.  
  12.  
  13. 		static int Main( string[] args )
  14. 		{
  15. 			bool debug = false;
  16.  
  17. 			#region Parse command line
  18.  
  19. 			switch ( args.Length )
  20. 			{
  21. 				case 0:
  22. 					return ShowHelp( string.Empty );
  23. 				case 2:
  24. 					break;
  25. 				case 3:
  26. 					if ( args[2].Substring( 0, 2 ).ToUpper( ) == "/D" )
  27. 					{
  28. 						debug = true;
  29. 					}
  30. 					else
  31. 					{
  32. 						return ShowHelp( "Invalid command line argument(s)" );
  33. 					}
  34. 					break;
  35. 				default:
  36. 					return ShowHelp( "Invalid number of command line arguments" );
  37. 			}
  38.  
  39. 			#endregion Parse command line
  40.  
  41. 			try
  42. 			{
  43. 				#region Validate command line arguments
  44.  
  45. 				string sourcefile = args[0];
  46. 				// Check if a source file was specified
  47. 				if ( string.IsNullOrWhiteSpace( sourcefile ) )
  48. 				{
  49. 					return ShowHelp( "Invalid source file specification" );
  50. 				}
  51. 				// Check if the source file name is valid, and make sure to use its full path
  52. 				try
  53. 				{
  54. 					sourcefile = Path.GetFullPath( sourcefile ).Trim( '"' );
  55. 				}
  56. 				catch ( ArgumentException )
  57. 				{
  58. 					return ShowHelp( "No wildcards allowed in source file" );
  59. 				}
  60. 				// Check if the source file exists
  61. 				if ( !File.Exists( sourcefile ) )
  62. 				{
  63. 					return ShowHelp( "File not found: \"{0}\"", sourcefile );
  64. 				}
  65.  
  66. 				string targetspec = args[1];
  67. 				if ( string.IsNullOrWhiteSpace( targetspec ) )
  68. 				{
  69. 					return ShowHelp( "Invalid target file specification" );
  70. 				}
  71. 				// Check if the target directory exists
  72. 				string targetdir = string.Empty;
  73. 				try
  74. 				{
  75. 					targetdir = Path.GetDirectoryName( targetspec );
  76. 					if ( string.IsNullOrWhiteSpace( targetdir ) )
  77. 					{
  78. 						targetdir = Path.GetFullPath( "." );
  79. 					}
  80. 				}
  81. 				catch ( ArgumentException )
  82. 				{
  83. 					return ShowHelp( "Target folder not found: \"{0}\"", targetspec );
  84. 				}
  85.  
  86. 				#endregion Validate command line arguments
  87.  
  88. 				// Extract the FILE specification (removing the path)
  89. 				string targetfilespec = targetspec.Substring( targetspec.LastIndexOf( "\\" ) + 1 );
  90. 				string[] targetfiles = Directory.EnumerateFiles( targetdir, targetfilespec ).ToArray<string>( );
  91. 				DateTime timestamp = File.GetLastWriteTime( sourcefile );
  92. 				int count = 0;
  93. 				int rc = 0;
  94. 				foreach ( string targetfile in targetfiles )
  95. 				{
  96. 					if ( targetfile.ToUpper( ) != sourcefile.ToUpper( ) )
  97. 					{
  98. 						count++;
  99. 						if ( debug )
  100. 						{
  101. 							Console.WriteLine( "File   : {0}", targetfile );
  102. 							Console.WriteLine( "Before : {0}", File.GetLastWriteTime( targetfile ) );
  103. 						}
  104. 						try
  105. 						{
  106. 							File.SetLastWriteTime( targetfile, timestamp );
  107. 						}
  108. 						catch ( Exception e )
  109. 						{
  110. 							rc = 1;
  111. 							if ( debug )
  112. 							{
  113. 								Console.WriteLine( "Error  : {0}", e.Message );
  114. 							}
  115. 							else
  116. 							{
  117. 								Console.Error.WriteLine( "File   : {0}", targetfile );
  118. 								Console.Error.WriteLine( "Error  : {0}", e.Message );
  119. 							}
  120. 						}
  121. 						if ( debug )
  122. 						{
  123. 							Console.WriteLine( "After  : {0}", File.GetLastWriteTime( targetfile ) );
  124. 							Console.WriteLine( );
  125. 						}
  126. 					}
  127. 				}
  128.  
  129. 				if ( debug )
  130. 				{
  131. 					Console.WriteLine( "{0} matching file{1}", count, ( count == 1 ? "" : "s" ) );
  132. 				}
  133.  
  134. 				if ( count == 0 )
  135. 				{
  136. 					return ShowHelp( "No matching target files: \"{0}\"", targetspec );
  137. 				}
  138.  
  139. 				return rc;
  140. 			}
  141. 			catch ( Exception e )
  142. 			{
  143. 				return ShowHelp( e.Message );
  144. 			}
  145. 		}
  146.  
  147.  
  148. 		public static int ShowHelp( params string[] errmsg )
  149. 		{
  150. 			/*
  151. 			CloneDate.exe,  Version 1.00
  152. 			Modify the LastModified date (timestamp) of the target file(s) to
  153. 			match the specified source file's timestamp
  154.  
  155. 			Usage:    CloneDate.exe  sourcefile  targetfiles  [ /Debug ]
  156.  
  157. 			Where:    sourcefile     is the file whose timestamp is to be cloned
  158. 			          targetfiles    are the files whose timestamp are to be modified
  159. 			                         (single filespec, wildcards * and ? are allowed)
  160. 			          /Debug         displays file name and timestamps before and
  161. 			                         after modification for each matching file
  162.  
  163. 			Example:  CloneDate.exe C:\boot.ini C:\test.log
  164. 			          will change C:\test.log's timestamp to match C:\boot.ini's
  165.  
  166. 			Notes:    Target filespec may include sourcefile (sourcefile will be skipped).
  167. 			          Always be careful when using wildcards; they may also return matching
  168. 			          "short" (8.3) file names (for backwards compatibility with FAT16).
  169.  
  170. 			Written by Rob van der Woude
  171. 			http://www.robvanderwoude.com
  172. 			*/
  173.  
  174. 			if ( errmsg.Length > 0 )
  175. 			{
  176. 				List<string> errargs = new List<string>( errmsg );
  177. 				errargs.RemoveAt( 0 );
  178. 				Console.Error.WriteLine( );
  179. 				Console.ForegroundColor = ConsoleColor.Red;
  180. 				Console.Error.Write( "ERROR:\t" );
  181. 				Console.ForegroundColor = ConsoleColor.White;
  182. 				Console.Error.WriteLine( errmsg[0], errargs.ToArray( ) );
  183. 				Console.ResetColor( );
  184. 			}
  185.  
  186. 			Console.Error.WriteLine( );
  187.  
  188. 			Console.Error.WriteLine( "CloneDate.exe,  Version {0}", progver );
  189.  
  190. 			Console.Error.WriteLine( "Modify the LastModified date (timestamp) of the target" );
  191.  
  192. 			Console.Error.WriteLine( "file(s) to match the specified source file's timestamp" );
  193.  
  194. 			Console.Error.WriteLine( );
  195.  
  196. 			Console.Error.Write( "Usage:    " );
  197. 			Console.ForegroundColor = ConsoleColor.White;
  198. 			Console.Error.WriteLine( "CloneDate.exe  sourcefile  targetfiles" );
  199. 			Console.ResetColor( );
  200.  
  201. 			Console.Error.WriteLine( );
  202.  
  203. 			Console.Error.Write( "Where:    " );
  204. 			Console.ForegroundColor = ConsoleColor.White;
  205. 			Console.Error.Write( "sourcefile" );
  206. 			Console.ResetColor( );
  207. 			Console.Error.WriteLine( "     is the file whose timestamp is to be cloned" );
  208.  
  209. 			Console.ForegroundColor = ConsoleColor.White;
  210. 			Console.Error.Write( "          targetfiles" );
  211. 			Console.ResetColor( );
  212. 			Console.Error.WriteLine( "    are the files whose timestamp are to be modified" );
  213.  
  214. 			Console.Error.WriteLine( "                         (single filespec, wildcards * and ? are allowed)" );
  215.  
  216. 			Console.ForegroundColor = ConsoleColor.White;
  217. 			Console.Error.Write( "          /D" );
  218. 			Console.ResetColor( );
  219. 			Console.Error.WriteLine( "ebug         displays file name and timestamps before and" );
  220.  
  221. 			Console.Error.WriteLine( "                         after modification for each matching file" );
  222.  
  223. 			Console.Error.WriteLine( );
  224.  
  225. 			Console.Error.Write( "Example:  " );
  226. 			Console.ForegroundColor = ConsoleColor.White;
  227. 			Console.Error.WriteLine( "CloneDate.exe C:\\boot.ini C:\\test.log" );
  228. 			Console.ResetColor( );
  229.  
  230. 			Console.Error.WriteLine( "          will change C:\\test.log's timestamp to match C:\\boot.ini's" );
  231.  
  232. 			Console.Error.WriteLine( );
  233.  
  234. 			Console.Error.WriteLine( "Notes:    Target filespec may include sourcefile (sourcefile will be skipped)." );
  235.  
  236. 			Console.Error.WriteLine( "          Always be careful when using wildcards; they may also return matching" );
  237.  
  238. 			Console.Error.WriteLine( "          \"short\" (8.3) file names (for backwards compatibility with FAT16)." );
  239.  
  240. 			Console.Error.WriteLine( );
  241.  
  242. 			Console.Error.WriteLine( "Written by Rob van der Woude" );
  243.  
  244. 			Console.Error.WriteLine( "http://www.robvanderwoude.com" );
  245.  
  246. 			return 1;
  247. 		}
  248.  
  249. 	}
  250. }
  251.  

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