private static void Main(string[] args) { if (args.Length < 1) { Console.WriteLine("Source and Destination directory missing"); Console.WriteLine("Usage: ImageSorter.exe <source dir> <destination dir> [params](optional)"); Console.WriteLine("Params:"); Console.WriteLine("-whatif \t Runs the script and displays output without commiting the canges."); Console.WriteLine("-d \t Deletes the source files sorting and copying the files to the destination folder."); LogUtility.WriteToLog("No Params", LogUtility.Level.Error); return; } GetArgs(args); Validation.ValidateDirectory(_sourceDir); Validation.ValidateDirectory(_destinationDir); //Get files from source dir Print("----------- Image sorting started -----------"); Print("Getting files..."); var files = GetAllFiles(_sourceDir); Print("Done getting files"); //might do an unneccecary list loop //LogUtility.WriteToLog("Files to move: " + files.Count(), LogUtility.Level.Info); //Loop thru files foreach (var file in files) { //For each file read date from exif var fileDate = ParsePhotoDate(file); if (!fileDate.HasValue) { continue; } //Create new paths var newDesitnationFolder = GetNewDestinationFolder(_destinationDir, fileDate); var duplicateDestinationFolder = GetDuplicateDestinationFolder(_destinationDir, fileDate); var newFullPath = newDesitnationFolder + Path.GetFileName(file); Print(file + " ==> " + newFullPath); if (_whatIf) { Print(file + " ==> " + newFullPath); _movedFiles.Add(file); continue; } CopyFile(newDesitnationFolder, newFullPath, duplicateDestinationFolder, file); } Print(_movedFiles.Count() + " copied to new folders"); if (_deleteSource) { DeleteCopiedFiles(_movedFiles, _whatIf); } Print("----------- Image sorting finished -----------"); Print(_movedFiles.Count() + " files have been sorted to the new " + _destinationDir); Print("See the log for details"); Console.ReadLine(); }
private static DateTime?ParsePhotoDate(string path) { TagLib.File file = null; //Get CR2 EXIF date if (Path.GetExtension(path) == ".CR2") { return(GetRC2PhotoDate(path)); } try { file = TagLib.File.Create(path); } catch (UnsupportedFormatException) { Print("UNSUPPORTED FILE not moving: " + path); return(null); } catch (CorruptFileException) { var time = LastWriteTime(path); Console.WriteLine("---------------"); Print("Corrupted File, using last Write time " + path + time); LogUtility.WriteToLog("Corrupted File, using last Write time " + path + time, LogUtility.Level.Error); Console.WriteLine("---------------"); return(LastWriteTime(path)); } var image = file as TagLib.Image.File; if (image == null) { Print("NOT AN IMAGE FILE Using file date: " + path); return(LastWriteTime(path)); } return(image.ImageTag.DateTime ?? LastWriteTime(path)); //Console.WriteLine(String.Empty); //Console.WriteLine(path); //Console.WriteLine(String.Empty); //Console.WriteLine("Tags in object : " + image.TagTypes); //Console.WriteLine(String.Empty); //Console.WriteLine("Comment : " + image.ImageTag.Comment); //Console.Write("Keywords : "); //foreach (var keyword in image.ImageTag.Keywords) //{ // Console.Write(keyword + " "); //} //Console.WriteLine(); //Console.WriteLine("Rating : " + image.ImageTag.Rating); //Console.WriteLine("DateTime : " + image.ImageTag.DateTime); //Console.WriteLine("Orientation : " + image.ImageTag.Orientation); //Console.WriteLine("Software : " + image.ImageTag.Software); //Console.WriteLine("ExposureTime : " + image.ImageTag.ExposureTime); //Console.WriteLine("FNumber : " + image.ImageTag.FNumber); //Console.WriteLine("ISOSpeedRatings : " + image.ImageTag.ISOSpeedRatings); //Console.WriteLine("FocalLength : " + image.ImageTag.FocalLength); //Console.WriteLine("FocalLength35mm : " + image.ImageTag.FocalLengthIn35mmFilm); //Console.WriteLine("Make : " + image.ImageTag.Make); //Console.WriteLine("Model : " + image.ImageTag.Model); //if (image.Properties != null) //{ // Console.WriteLine("Width : " + image.Properties.PhotoWidth); // Console.WriteLine("Height : " + image.Properties.PhotoHeight); // Console.WriteLine("Type : " + image.Properties.Description); //} //Console.WriteLine(); //Console.WriteLine("Writable? : " + image.Writeable.ToString()); //Console.WriteLine("Corrupt? : " + image.PossiblyCorrupt.ToString()); //if (image.PossiblyCorrupt) //{ // foreach (string reason in image.CorruptionReasons) // { // Console.WriteLine(" * " + reason); // } //} //Console.WriteLine("---------------------------------------"); }
private static void Print(string message) { Console.WriteLine(message); LogUtility.WriteToLog(message, LogUtility.Level.Info); }