Powered by GeSHi

Source code for capturedate.cs

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

  1. using System;
  2. using System.IO;
  3. using System.Text.RegularExpressions;
  4.  
  5. namespace RobvanderWoude
  6. {
  7. 	class CaptureDate
  8. 	{
  9. 		static int Main( string[] args )
  10. 		{
  11. 			if ( args.Length == 0 )
  12. 			{
  13. 				return WriteError( );
  14. 			}
  15.  
  16. 			if ( args.Length > 2 )
  17. 			{
  18. 				return WriteError( "Too many command line arguments" );
  19. 			}
  20.  
  21. 			string filename = string.Empty;
  22. 			bool timestamp = false;
  23. 			bool fileset = false;
  24.  
  25. 			foreach ( string arg in args )
  26. 			{
  27. 				if ( arg == "/?" )
  28. 				{
  29. 					return WriteError( );
  30. 				}
  31. 				if ( arg[0] == '/' )
  32. 				{
  33. 					if ( arg.ToUpper( ) == "/T" )
  34. 					{
  35. 						if ( timestamp )
  36. 						{
  37. 							return WriteError( "Duplicate switch: \"/T\"" );
  38. 						}
  39. 						timestamp = true;
  40. 					}
  41. 					else
  42. 					{
  43. 						return WriteError( "Invalid switch: \"" + arg + "\"" );
  44. 					}
  45. 				}
  46. 				else
  47. 				{
  48. 					if ( fileset )
  49. 					{
  50. 						return WriteError( "Invalid or duplicate argument(s)" );
  51. 					}
  52. 					filename = arg;
  53. 					fileset = true;
  54. 					if ( !Directory.Exists( Path.GetDirectoryName( filename ) ) )
  55. 					{
  56. 						return WriteError( "Folder not found: \"" + Path.GetDirectoryName( filename ) + "\"" );
  57. 					}
  58. 					if ( !File.Exists( filename ) )
  59. 					{
  60. 						return WriteError( "File not found: \"" + filename + "\"" );
  61. 					}
  62. 				}
  63. 			}
  64. 			if ( string.IsNullOrEmpty( filename ) )
  65. 			{
  66. 				return WriteError( "No file specified" );
  67. 			}
  68.  
  69. 			try
  70. 			{
  71. 				StreamReader file = new StreamReader( filename );
  72. 				char[] buffer = new char[1024];
  73. 				int result = file.Read( buffer, 0, 1023 );
  74. 				file.Close( );
  75. 				string header = new string( buffer );
  76.  
  77. 				if ( string.IsNullOrEmpty( header ) )
  78. 				{
  79. 					return WriteError( "Could not open file \"" + filename + "\"" );
  80. 				}
  81.  
  82. 				string pattern = "\\b[12]\\d{3}:[01]\\d:[0-3]\\d [0-2]\\d:[0-5]\\d:[0-5]\\d\\b";
  83. 				Regex regexp = new Regex( pattern, RegexOptions.None );
  84. 				MatchCollection matches = regexp.Matches( header );
  85. 				string photoDate = string.Empty;
  86. 				if ( matches.Count == 0 )
  87. 				{
  88. 					Console.Error.WriteLine( "No capture date found in file header" );
  89. 					return WriteError( );
  90. 				}
  91. 				foreach ( Match match in matches )
  92. 				{
  93. 					if ( string.IsNullOrEmpty( photoDate ) || String.Compare( photoDate, match.Value, StringComparison.Ordinal ) > 0 )
  94. 					{
  95. 						photoDate = match.Value;
  96. 					}
  97. 				}
  98. 				if ( timestamp )
  99. 				{
  100. 					photoDate = photoDate.Replace( ":", "" ).Replace( " ", "T" );
  101. 				}
  102. 				else
  103. 				{
  104. 					photoDate = photoDate.Substring( 0, 10 ).Replace( ":", "-" ) + photoDate.Substring( 10 );
  105. 				}
  106. 				Console.WriteLine( photoDate );
  107. 				return 0;
  108. 			}
  109. 			catch ( Exception e )
  110. 			{
  111. 				return WriteError( e.Message );
  112. 			}
  113. 		}
  114.  
  115. 		static string Today( bool useDelims = true )
  116. 		{
  117. 			string dateFormat;
  118. 			if ( useDelims )
  119. 			{
  120. 				dateFormat = "{0:yyyy}-{0:MM}-{0:dd} {0:HH}:{0:mm}:{0:ss}";
  121. 			}
  122. 			else
  123. 			{
  124. 				dateFormat = "{0:yyyy}{0:MM}{0:dd}T{0:HH}{0:mm}{0:ss}";
  125. 			}
  126. 			return String.Format( dateFormat, DateTime.Now );
  127. 		}
  128.  
  129. 		#region Error handling
  130.  
  131. 		public static int WriteError( string errorMessage = "" )
  132. 		{
  133. 			/*
  134. 			CaptureDate,  Version 1.00
  135. 			Return the capture date and time for the specified image file
  136.  
  137. 			Usage:   CAPTUREDATE  image  [ /T ]
  138.  
  139. 			Where:   image        specifies the image file
  140. 			         /T           return the timestamp without "-" and ":" delimiters,
  141. 			                      e.g. 20090516T135628 instead of 2009-05-16 13:56:28
  142.  
  143. 			Notes:   Result will be displayed on screen, e.g. 2009-05-16 13:56:28.
  144. 			         The date/time is extracted by searching for the earliest date/time in
  145. 			         the first 1024 bytes of the image file.
  146. 			         Return code ("errorlevel") is 0 if a valid date was found, otherwise 1.
  147.  
  148. 			Written by Rob van der Woude
  149. 			http://www.robvanderwoude.com
  150. 			*/
  151.  
  152. 			Console.ResetColor( );
  153. 			if ( string.IsNullOrEmpty( errorMessage ) == false )
  154. 			{
  155. 				Console.Error.WriteLine( );
  156. 				Console.ForegroundColor = ConsoleColor.Red;
  157. 				Console.Error.Write( "ERROR:  " );
  158. 				Console.ForegroundColor = ConsoleColor.White;
  159. 				Console.Error.WriteLine( errorMessage );
  160. 				Console.ResetColor( );
  161. 			}
  162. 			string timestamp = Today( true );
  163. 			string timestampNodelims = Today( false );
  164. 			Console.Error.WriteLine( );
  165. 			Console.Error.WriteLine( "CaptureDate,  Version 1.00" );
  166. 			Console.Error.WriteLine( "Return the capture date and time for the specified image file" );
  167. 			Console.Error.WriteLine( );
  168. 			Console.Error.Write( "Usage:   " );
  169. 			Console.ForegroundColor = ConsoleColor.White;
  170. 			Console.Error.WriteLine( "CAPTUREDATE  image  [ /T ]" );
  171. 			Console.ResetColor( );
  172. 			Console.Error.WriteLine( );
  173. 			Console.Error.Write( "Where:   " );
  174. 			Console.ForegroundColor = ConsoleColor.White;
  175. 			Console.Error.Write( "image" );
  176. 			Console.ResetColor( );
  177. 			Console.Error.WriteLine( "        specifies the image file" );
  178. 			Console.ForegroundColor = ConsoleColor.White;
  179. 			Console.Error.Write( "         /T" );
  180. 			Console.ResetColor( );
  181. 			Console.Error.WriteLine( "           return the timestamp without \"-\" and \":\" delimiters," );
  182. 			Console.Error.Write( "                      e.g. " );
  183. 			Console.ForegroundColor = ConsoleColor.White;
  184. 			Console.Error.Write( timestampNodelims );
  185. 			Console.ResetColor( );
  186. 			Console.Error.Write( " instead of " );
  187. 			Console.ForegroundColor = ConsoleColor.White;
  188. 			Console.Error.Write( timestamp );
  189. 			Console.ResetColor( );
  190. 			Console.Error.WriteLine( "." );
  191. 			Console.Error.WriteLine( );
  192. 			Console.Error.Write( "Notes:   Result will be displayed on screen, e.g. " );
  193. 			Console.ForegroundColor = ConsoleColor.White;
  194. 			Console.Error.Write( timestamp );
  195. 			Console.ResetColor( );
  196. 			Console.Error.WriteLine( "." );
  197. 			Console.Error.WriteLine( "         The date/time is extracted by searching for the earliest date/time in" );
  198. 			Console.Error.WriteLine( "         the first 1024 bytes of the image file." );
  199. 			Console.Error.Write( "         Return code (\"errorlevel\") is " );
  200. 			Console.ForegroundColor = ConsoleColor.White;
  201. 			Console.Error.Write( "0" );
  202. 			Console.ResetColor( );
  203. 			Console.Error.Write( " if a valid date was found, otherwise " );
  204. 			Console.ForegroundColor = ConsoleColor.White;
  205. 			Console.Error.WriteLine( "1" );
  206. 			Console.ResetColor( );
  207. 			Console.Error.WriteLine( );
  208. 			Console.Error.WriteLine( "Written by Rob van der Woude" );
  209. 			Console.Error.WriteLine( "http://www.robvanderwoude.com" );
  210. 			return 1;
  211. 		}
  212.  
  213. 		#endregion Error handling
  214.  
  215. 	}
  216. }
  217.