using System; using System.IO; using System.Text.RegularExpressions; namespace RobvanderWoude { class CameraModel { static string progver = "2.01"; static int Main( string[] args ) { if ( args.Length == 0 || args.Length > 2 ) { return ShowHelp( ); } string filename = string.Empty; string make = string.Empty; foreach ( string arg in args ) { if ( arg == "/?" ) { return ShowHelp( ); } if ( arg[0] == '/' ) { if ( arg.Length > 2 && arg.ToUpper( ).Substring( 0, 3 ) == "/M:" ) { make = arg.Substring( 3 ).Replace( "\"", "" ); } else { return ShowHelp( "Invalid argument: \"" + arg + "\"" ); } } else { filename = arg; if ( !Directory.Exists( Path.GetDirectoryName( filename ) ) ) { return ShowHelp( "Folder not found: \"" + Path.GetDirectoryName( filename ) + "\"" ); } if ( !File.Exists( filename ) ) { return ShowHelp( "File not found: \"" + filename + "\"" ); } } } if ( string.IsNullOrEmpty( filename ) ) { return ShowHelp( "No file specified" ); } try { StreamReader file = new StreamReader( filename ); char[] buffer = new char[1024]; int result = file.Read( buffer, 0, 1023 ); file.Close( ); string header = new string( buffer ); if ( string.IsNullOrEmpty( header ) ) { return ShowHelp( "Could not open file \"" + filename + "\"" ); } Regex regexp = null; string pattern = string.Empty; if ( !string.IsNullOrEmpty( make ) ) { pattern = "\\b" + make + "\\b"; regexp = new Regex( pattern, RegexOptions.IgnoreCase ); if ( !regexp.IsMatch( header ) ) { return 0; } } if ( string.IsNullOrEmpty( make ) ) { pattern = "\\b(Canon (DIGITAL IXUS ([0-9]\\d*|[A-Z\\d]+)( [A-Z]+)?|PowerShot D[1-9]\\d*|EOS [1-9]\\d{0,3}D)|NIKON D[1-9]\\d{1,3}|PENTAX K\\-?(\\d+[A-Z]*|m)|SAMSUNG[\\s\\000][\\s\\w-]*)"; } else { pattern = "\\b" + make + "[\\000\\s][\\dA-Z\\-]*\\d[\\dA-Z\\-]*\\b"; } regexp = new Regex( pattern, RegexOptions.IgnoreCase ); Match match1 = regexp.Match( header ); if ( !match1.Success ) { pattern = "\\b" + make + " [\\dA-Z\\-]+\\b"; regexp = new Regex( pattern, RegexOptions.IgnoreCase ); match1 = regexp.Match( header ); } if ( match1.Success ) { Console.WriteLine( match1.Value ); if ( Regex.IsMatch( match1.Value, "\\d" ) ) { regexp = new Regex( "\\d+" ); Match match2 = regexp.Match( match1.Value ); return Convert.ToInt32( match2.Value ); } else { return 0; } } Console.Error.WriteLine( "File is not made with a supported or specified camera" ); return ShowHelp( ); } catch ( Exception e ) { return ShowHelp( e.Message ); } } #region Error handling public static int ShowHelp( string errorMessage = "" ) { /* CameraModel, Version 2.01 Return the camera brand and model for the specified image file Usage: CAMERAMODEL image [ /M:make ] Where: image is the image file to check /M:make specifies camera brand to look for (e.g. /M:Pentax) Notes: Works for unmodified (Canon) RAW or JPG images, others not guaranteed. Result returned on screen (e.g. NIKON D60) and as errorlevel (e.g. 60). Errorlevel 0 if image not found, or not made by supported or specified camera brand, or camera model doesn't contain numbers (e.g. PENTAX K-m). Written by Rob van der Woude http://www.robvanderwoude.com */ Console.ResetColor( ); if ( string.IsNullOrEmpty( errorMessage ) == false ) { Console.Error.WriteLine( ); Console.ForegroundColor = ConsoleColor.Red; Console.Error.Write( "ERROR:\t" ); Console.ForegroundColor = ConsoleColor.White; Console.Error.WriteLine( errorMessage ); Console.ResetColor( ); } Console.Error.WriteLine( ); Console.Error.WriteLine( "CameraModel, Version {0}", progver ); Console.Error.WriteLine( "Return the camera brand and model for the specified image file" ); Console.Error.WriteLine( ); Console.Error.Write( "Usage: " ); Console.ForegroundColor = ConsoleColor.White; Console.Error.WriteLine( "CAMERAMODEL image [ /M:make ]" ); Console.ResetColor( ); Console.Error.WriteLine( ); Console.Error.Write( "Where: " ); Console.ForegroundColor = ConsoleColor.White; Console.Error.Write( "image" ); Console.ResetColor( ); Console.Error.WriteLine( " is the image file to check" ); Console.ForegroundColor = ConsoleColor.White; Console.Error.Write( " /M:make" ); Console.ResetColor( ); Console.Error.WriteLine( " specifies camera brand to look for (e.g. /M:Pentax)" ); Console.Error.WriteLine( ); Console.Error.WriteLine( "Notes: Works for unmodified (Canon) RAW or JPG images, others not guaranteed." ); Console.Error.Write( " Result returned on screen (e.g. " ); Console.ForegroundColor = ConsoleColor.White; Console.Error.Write( "NIKON D60" ); Console.ResetColor( ); Console.Error.Write( ") and as errorlevel (e.g. " ); Console.ForegroundColor = ConsoleColor.White; Console.Error.Write( "60" ); Console.ResetColor( ); Console.Error.WriteLine( ")." ); Console.Error.Write( " Errorlevel " ); Console.ForegroundColor = ConsoleColor.White; Console.Error.Write( "0" ); Console.ResetColor( ); Console.Error.WriteLine( " if image not found or not made by supported or specified" ); Console.Error.WriteLine( " camera brand, or camera model doesn't contain numbers (e.g. PENTAX K-m)." ); Console.Error.WriteLine( ); Console.Error.WriteLine( "Written by Rob van der Woude" ); Console.Error.WriteLine( "http://www.robvanderwoude.com" ); return 0; } #endregion Error handling } }