static void Main(string[] args) { if (!ReadArgs(args)) { return; } Console.WriteLine("Directory: {0} Files: {1}", directory, files); if (!Directory.Exists(directory)) { Console.WriteLine("Given directory {0} is invalid", directory); } string[] filenames = Directory.GetFiles(directory, files, doSubFolders ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly); List <string> badFiles = new List <string>(); List <string> collisions = new List <string>(); StreamWriter log = File.AppendText("jpegsort.log"); log.WriteLine(":: {0}", DateTime.Now); log.WriteLine(":: {0}", Environment.CommandLine); foreach (string filename in filenames) { bool haveTime = false; DateTime dt = new DateTime(); try { JpegInfo ji = new JpegInfo(filename); { if (ji.HaveOriginalTime) { dt = ji.OriginalTime; haveTime = true; } } } catch (Exception e) { //Console.WriteLine(" Exception: {0} in file {1}", e.Message, filename); } if (haveTime) { string yearDirectory = string.Format("{0,4}", dt.Year); string monthDirectory = string.Format("{0}\\{1,2:00}", yearDirectory, dt.Month); string dayDirectory = string.Format("{0}\\{1,2:00}", monthDirectory, dt.Day); if (!Directory.Exists(yearDirectory)) { Directory.CreateDirectory(yearDirectory); } if (!Directory.Exists(monthDirectory)) { Directory.CreateDirectory(monthDirectory); } if (!Directory.Exists(dayDirectory)) { Directory.CreateDirectory(dayDirectory); } string newFileLocation = string.Format("{0}\\{1}", dayDirectory, new FileInfo(filename).Name); if (!File.Exists(newFileLocation)) { log.WriteLine("move {0} {1}", filename, newFileLocation); Console.WriteLine("{0}", newFileLocation); File.Move(filename, newFileLocation); } else { JpegInfo jiExisting = new JpegInfo(newFileLocation); bool resolvedCollision = false; if (jiExisting.HaveOriginalTime && DateTime.Compare(dt, jiExisting.OriginalTime) == 0) { if (deleteDuplicates) { Console.WriteLine("{0} already exists, times match - Deleting {1}", newFileLocation, filename); log.WriteLine(":: {0} already exists, times match - Deleting {1}", newFileLocation, filename); File.Delete(filename); resolvedCollision = true; } else { Console.WriteLine("{0} already exists, times match", newFileLocation); } } else { Console.WriteLine("{0} already exists, times do not match", newFileLocation); } if (!resolvedCollision) { collisions.Add(filename); } } } else { badFiles.Add(filename); } } Console.WriteLine("{0} files processed", filenames.Length); if (badFiles.Count > 0) { Console.WriteLine("{0} file{1} had no original date", badFiles.Count, badFiles.Count > 1 ? "s":""); Console.WriteLine("Unsorted files:"); foreach (string str in badFiles) { Console.WriteLine(str); } } if (collisions.Count > 0) { Console.WriteLine("{0} file{1} already in destination folder", collisions.Count, collisions.Count > 1 ? "s" : ""); Console.WriteLine("Unresolved Collisions:"); foreach (string str in collisions) { Console.WriteLine(str); } } log.WriteLine(); log.Close(); }
static void Main(string[] args) { if (!ReadArgs(args)) { return; } string[] filenames = Directory.GetFiles(".", filespec, doSubFolders ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly); foreach (string filename in filenames) { Console.WriteLine("File: {0}", filename); try { JpegInfo ji = new JpegInfo(filename); Console.WriteLine("File size: {0}", ji.FileSize); Console.WriteLine("Camera make: {0}", ji.CameraMake); Console.WriteLine("Camera model: {0}", ji.CameraModel); Console.WriteLine("Camera orientation: {0}", ji.CameraOrientation); if (ji.HaveFileChangeTime) { Console.WriteLine("File change time: {0}", ji.FileChangeTime.ToString("MM/dd/yyyy HH:mm:ss.fff")); } Console.WriteLine("Exposure: {0:0.0000}", ji.Exposure); Console.WriteLine("f-stop: {0:0.00}", ji.Fstop); Console.WriteLine("Focal length: {0:0.00}", ji.FocalLength); Console.WriteLine("ISO: {0}", ji.IsoSpeed); if (ji.HaveOriginalTime) { Console.WriteLine("Image taken: {0}", ji.OriginalTime.ToString("MM/dd/yyyy HH:mm:ss.fff")); } Console.WriteLine("Image width: {0}", ji.ImageWidth); Console.WriteLine("Image height: {0}", ji.ImageHeight); Console.WriteLine("Focal length (on 35mm): {0}", ji.FocalLengthOn35mm); double zoom = ji.Zoom; if (zoom != 0.0 && zoom != 1.0) { Console.WriteLine("Digital zoom: {0:0.0}:1", ji.Zoom); } if (ji.HaveGps) { Console.WriteLine("GPS coords: {0}", ji.GpsToString); Console.WriteLine("GPS altitude: {0:0.00}", ji.GpsAltitude); if (ji.HaveGpsTimeStamp) { Console.WriteLine("GPS Timestamp: {0} UTC", ji.GpsTimeStamp.ToString("MM/dd/yyyy HH:mm:ss.fff")); } } if (verbose) { Console.WriteLine("Exif Attributes:"); foreach (IfdElement ifd in ji.ExifAttributes.Values) { Console.WriteLine(ifd.ToString()); } } } catch (Exception e) { Console.WriteLine("Exception: {0}", e.Message); } } }