public static void OrganisePhotos(OrganiserCriteria criteria)
        {
            if (string.IsNullOrEmpty(criteria.SourcePath))
                throw new ArgumentNullException("source path");

            string sourcePath = criteria.SourcePath.TrimEnd('\\') + @"\";

            string destinationPathPattern = (criteria.DestinationPath ?? _defaultDestinationPath).TrimEnd('\\') + @"\";

            string destinationFilePattern = criteria.DestinationFile ?? _defaultDestinationFile;

            SearchOption searchOption = SearchOption.TopDirectoryOnly;
            if (criteria.SearchSubDirectories)
                searchOption = SearchOption.AllDirectories;

            string searchPattern = criteria.SearchPattern ?? _defaultSearchPattern;

            string[] files = System.IO.Directory.GetFiles(sourcePath, searchPattern, searchOption);

            int total = files.Count();
            if (criteria.KeepOriginal)
                Console.WriteLine(string.Format("Coping {0} file(s)...", total));
            else
                Console.WriteLine(string.Format("Moving {0} file(s)...", total));

            foreach (var filePath in files)
            {
                FileInfo file = new FileInfo(filePath);

                if (!file.Exists)
                    continue; //TODO: add to fail list - photo doesn't not found

                DateTime dateTime = GetPhotoTakenDate(file.FullName);

                if (dateTime == default(DateTime))
                    continue; //TODO: add to fail list - photo doesn't have a timestamp

                string destinationPath = string.Format(destinationPathPattern, dateTime).TrimEnd('\\') + "\\";
                if (!Path.IsPathRooted(destinationPath))
                    destinationPath = sourcePath + destinationPath;

                if (!criteria.ListOnly)
                    Directory.CreateDirectory(destinationPath);

                string originalFile = file.Name;

                string destinationFile = string.Format(destinationFilePattern, dateTime, file.Name.Substring(0, file.Name.Length - 4), file.Extension.Trim('.'));

                string newFilePath = destinationPath + destinationFile;

                if (File.Exists(newFilePath))
                    continue; //TODO: add to fail list - a file with the same name already exists at the output location

                if (criteria.KeepOriginal)
                {
                    if (criteria.ListOnly)
                        Console.WriteLine(string.Format("CopyTo: {0}", newFilePath));
                    else
                        file.CopyTo(newFilePath);
                }
                else
                {
                    if (criteria.ListOnly)
                        Console.WriteLine(string.Format("MoveTo: {0}", newFilePath));
                    else
                        file.MoveTo(newFilePath);
                }

                Console.WriteLine(total-- + " - " + originalFile);
                Console.WriteLine(" -> " + file.FullName);
            }
        }
Ejemplo n.º 2
0
        public static void OrganisePhotos(OrganiserCriteria criteria)
        {
            if (string.IsNullOrEmpty(criteria.SourcePath))
            {
                throw new ArgumentNullException("source path");
            }

            string sourcePath = criteria.SourcePath.TrimEnd('\\') + @"\";

            string destinationPathPattern = (criteria.DestinationPath ?? _defaultDestinationPath).TrimEnd('\\') + @"\";

            string destinationFilePattern = criteria.DestinationFile ?? _defaultDestinationFile;

            SearchOption searchOption = SearchOption.TopDirectoryOnly;

            if (criteria.SearchSubDirectories)
            {
                searchOption = SearchOption.AllDirectories;
            }

            string searchPattern = criteria.SearchPattern ?? _defaultSearchPattern;

            string[] files = System.IO.Directory.GetFiles(sourcePath, searchPattern, searchOption);

            int total = files.Count();

            if (criteria.KeepOriginal)
            {
                Console.WriteLine(string.Format("Coping {0} file(s)...", total));
            }
            else
            {
                Console.WriteLine(string.Format("Moving {0} file(s)...", total));
            }

            foreach (var filePath in files)
            {
                FileInfo file = new FileInfo(filePath);

                if (!file.Exists)
                {
                    continue; //TODO: add to fail list - photo doesn't not found
                }
                DateTime dateTime = GetPhotoTakenDate(file.FullName);

                if (dateTime == default(DateTime))
                {
                    continue; //TODO: add to fail list - photo doesn't have a timestamp
                }
                string destinationPath = string.Format(destinationPathPattern, dateTime).TrimEnd('\\') + "\\";
                if (!Path.IsPathRooted(destinationPath))
                {
                    destinationPath = sourcePath + destinationPath;
                }

                if (!criteria.ListOnly)
                {
                    Directory.CreateDirectory(destinationPath);
                }

                string originalFile = file.Name;

                string destinationFile = string.Format(destinationFilePattern, dateTime, file.Name.Substring(0, file.Name.Length - 4), file.Extension.Trim('.'));

                string newFilePath = destinationPath + destinationFile;

                if (File.Exists(newFilePath))
                {
                    continue; //TODO: add to fail list - a file with the same name already exists at the output location
                }
                if (criteria.KeepOriginal)
                {
                    if (criteria.ListOnly)
                    {
                        Console.WriteLine(string.Format("CopyTo: {0}", newFilePath));
                    }
                    else
                    {
                        file.CopyTo(newFilePath);
                    }
                }
                else
                {
                    if (criteria.ListOnly)
                    {
                        Console.WriteLine(string.Format("MoveTo: {0}", newFilePath));
                    }
                    else
                    {
                        file.MoveTo(newFilePath);
                    }
                }

                Console.WriteLine(total-- + " - " + originalFile);
                Console.WriteLine(" -> " + file.FullName);
            }
        }
Ejemplo n.º 3
0
        static void Main(string[] args)
        {
            Arguments cmdArgs = new Arguments(args);

            if (args.Count() == 0 || cmdArgs["?"] != null)
            {
                Console.WriteLine(@"
My Photo Organiser

Command line tool to sort photos into folders by date.
By default photos are moved to a sub folder by year, then by date. 
Example:
    "".\Photo1.jpg"" was taken 21-Jan-2011
    MyPhotoOrganiser.exe moves the photo to the following: "".\2011\20110121\Photo1.jpg""

Usage:
    MyPhotoOrganiser.exe <source> [/d <destination>] [/f <file format>] [/c] [/s] [/p] [/l]

Where:
    <source> - source path of photos to sort
    /d - (optional) destination path
        can include .Net string format {0} of the date photo was taken
        default is "".\{0:yyyy}\{0:yyyyMMdd}\""
        ie relative to path <source>\\<year>\\<year><month><day>\\
    /f - (optional) file format of organised file ie to give a new file name
        string format {0} date photo taken eg {0:yyyyMMdd_HHmmss}
        string format {1} file name
        string format {2} files extension
        default is original filename eg ""{1}.{2}""
        dont forget to include extension eg 
    /c - (optional) copy files instead of move
    /s - (optional) search sub directories
    /p - (optional) System.IO.Directory.GetFiles search pattern
        default is *.jpg
    /l - (optional) list only, don't change a thing!

Remarks:
    Destination path is relative to the source path not from where the tool is run; unless a full path is used.
    For the file format (/f) argument don't forget to include file extension eg ""{1}.{2}"".
    
Examples:
    MyPhotoOrganiser.exe ""C:\MyPhotos""
    MyPhotoOrganiser.exe ""C:\MyPhotos"" /c /l
    MyPhotoOrganiser.exe ""C:\MyPhotos"" /s /d ""C:\SortedPhotos\{0:yyyy}\{0:MM}""
    MyPhotoOrganiser.exe ""C:\MyPhotos"" /s /f ""{0:yyyyMMdd_HHmmss}{1}.{2}""
");
                return;
            }

            OrganiserCriteria criteria = new OrganiserCriteria();

            criteria.SourcePath = args[0];

            // old source argument left in for backwards compatability
            // new way is to pick source from args[0]
            if (cmdArgs["src"] != null)
            {
                criteria.SourcePath = cmdArgs["src"];
            }

            criteria.SearchSubDirectories = (cmdArgs["s"] != null);

            criteria.ListOnly = (cmdArgs["l"] != null);

            if (cmdArgs["p"] != null)
            {
                criteria.SearchPattern = cmdArgs["p"];
            }

            if (cmdArgs["c"] != null)
            {
                criteria.KeepOriginal = true;
            }

            if (cmdArgs["d"] != null)
            {
                criteria.DestinationPath = cmdArgs["d"];
            }
            // where {0} is Date Photo Taken
            // @"C:\Documents and Settings\Mouters\My Documents\Camera\2008 Florida\{0:yyyy}\{0:yyyyMMdd}";

            if (cmdArgs["f"] != null)
            {
                criteria.DestinationFile = cmdArgs["f"];
            }
            // where {0} is Date Photo Taken; {1} filename; {2} ext
            // @"{0:yyyyMMdd_HHmmss}.{2}";

            PhotoOrganiserEngine.OrganisePhotos(criteria);
        }
Ejemplo n.º 4
0
        static void Main(string[] args)
        {
            Arguments cmdArgs = new Arguments(args);

            if (args.Count() == 0 || cmdArgs["?"] != null)
            {
                Console.WriteLine(@"
My Photo Organiser

Command line tool to sort photos into folders by date.
By default photos are moved to a sub folder by year, then by date. 
Example:
    "".\Photo1.jpg"" was taken 21-Jan-2011
    MyPhotoOrganiser.exe moves the photo to the following: "".\2011\20110121\Photo1.jpg""

Usage:
    MyPhotoOrganiser.exe <source> [/d <destination>] [/f <file format>] [/c] [/s] [/p] [/l]

Where:
    <source> - source path of photos to sort
    /d - (optional) destination path
        can include .Net string format {0} of the date photo was taken
        default is "".\{0:yyyy}\{0:yyyyMMdd}\""
        ie relative to path <source>\\<year>\\<year><month><day>\\
    /f - (optional) file format of organised file ie to give a new file name
        string format {0} date photo taken eg {0:yyyyMMdd_HHmmss}
        string format {1} file name
        string format {2} files extension
        default is original filename eg ""{1}.{2}""
        dont forget to include extension eg 
    /c - (optional) copy files instead of move
    /s - (optional) search sub directories
    /p - (optional) System.IO.Directory.GetFiles search pattern
        default is *.jpg
    /l - (optional) list only, don't change a thing!

Remarks:
    Destination path is relative to the source path not from where the tool is run; unless a full path is used.
    For the file format (/f) argument don't forget to include file extension eg ""{1}.{2}"".
    
Examples:
    MyPhotoOrganiser.exe ""C:\MyPhotos""
    MyPhotoOrganiser.exe ""C:\MyPhotos"" /c /l
    MyPhotoOrganiser.exe ""C:\MyPhotos"" /s /d ""C:\SortedPhotos\{0:yyyy}\{0:MM}""
    MyPhotoOrganiser.exe ""C:\MyPhotos"" /s /f ""{0:yyyyMMdd_HHmmss}{1}.{2}""
");
                return;
            }

            OrganiserCriteria criteria = new OrganiserCriteria();

            criteria.SourcePath = args[0];

            // old source argument left in for backwards compatability
            // new way is to pick source from args[0]
            if (cmdArgs["src"] != null)
                criteria.SourcePath = cmdArgs["src"];

            criteria.SearchSubDirectories = (cmdArgs["s"] != null);

            criteria.ListOnly = (cmdArgs["l"] != null);

            if (cmdArgs["p"] != null)
                criteria.SearchPattern = cmdArgs["p"];                

            if (cmdArgs["c"] != null)
                criteria.KeepOriginal = true;
            
            if (cmdArgs["d"] != null)
                criteria.DestinationPath = cmdArgs["d"];
            // where {0} is Date Photo Taken
            // @"C:\Documents and Settings\Mouters\My Documents\Camera\2008 Florida\{0:yyyy}\{0:yyyyMMdd}";

            if (cmdArgs["f"] != null)
                criteria.DestinationFile = cmdArgs["f"];
            // where {0} is Date Photo Taken; {1} filename; {2} ext
            // @"{0:yyyyMMdd_HHmmss}.{2}";

            PhotoOrganiserEngine.OrganisePhotos(criteria);
        }