0

I want to reorganize all the photos from one folder into subfolders of another path, where I want to create new subfolders named with the file creation dates.

Example:

photo1.png (creation date 12.02.2015)

photo2.png (creation date 12.02.2015)

photo3.png (creation date 13.02.2015)

--> create two subfolders: "12-feb-2015" with photo1.png and photo2.png and "13-feb-2015" with photo3.png

I wrote the code for copying the photos into the other folder and create subfolder with the current date. But I don't know how to create the subfolders named after the creation date of the files.

public class SimpleFileCopy
{
    static void Main(string[] args)
    {
        // Specify what is done when a file is changed, created, or deleted.
        string fileName = "*.png";
        string sourcePath = @"C:\tmp";
        string targetPath = @"U:\\";
        
        // Use Path class to manipulate file and directory paths.
        string sourceFile = Path.Combine(sourcePath, fileName);
        //string destFile = Path.Combine(Directory.CreateDirectory("U:\\" + DateTime.Now.ToString("dd-MMM-yyyy") , fileName);

        // To copy a folder's contents to a new location:
        // Create a new target folder, if necessary.
        if (!Directory.Exists("U:\\" + Directory.CreateDirectory("U:\\" + DateTime.Now.ToString("dd-MMM-yyyy"))))

        {
            Directory.CreateDirectory("U:\\" + Directory.CreateDirectory("U:\\" + DateTime.Now.ToString("dd-MMM-yyyy")));
        }
        else
        // To copy a file to another location and 
        // overwrite the destination file if it already exists.
        {

            foreach (var file in new DirectoryInfo(sourcePath).GetFiles(fileName))
            {
                try
                {
                    file.CopyTo(e.FullPath.Combine(targetPath + Directory.CreateDirectory("U:\\" + DateTime.Now.ToString("dd-MMM-yyyy")), file.Name));
                }
                catch { }
            }
        }
    }
}
Community
  • 1
  • 1
I.T.
  • 11
  • 4
  • Possible duplicate of [If a folder does not exist, create it](http://stackoverflow.com/questions/9065598/if-a-folder-does-not-exist-create-it) – Liam Oct 10 '16 at 08:47
  • Wait there...your already creating directory's? Your question make no sense then? What exactly are you stuck on? – Liam Oct 10 '16 at 08:48
  • You are creating dirs based on DateTime.Now, is that the base of your question? – H H Oct 10 '16 at 09:05
  • I didn't create the subdirectories. i need to create these named with the create date of the photos. I know it is a method GetCreationTime() but i don't know how to use it – I.T. Oct 10 '16 at 09:17

1 Answers1

0

You are having way to many Directory.CreateDirectory calls. Just enumerate your source folder files, then get the date with file.CreationTime, call Directory.CreateDirectory (no matter if it already exists) and then copy your file.

string fileName = "*.png";
string sourcePath = @"C:\tmp";
string targetPath = @"U:\";

foreach (var file in new DirectoryInfo(sourcePath).GetFiles(fileName))
{
    var targetFolderName = file.CreationTime.ToString("dd-MMM-yyyy");
    var dir = Directory.CreateDirectory(Path.Combine(targetPath, targetFolderName));
    file.CopyTo(Path.Combine(dir.FullName, file.Name), true);
}
grek40
  • 13,113
  • 1
  • 24
  • 50