Copying folders and files based on creation date

I am trying to copy all folders and their files from one folder to another. Here’s the scenario.
Copy all folders and their files and sub-folders (keeping the hierarchy intact) from c:\data1 and c:\data2 to d:\data1 and d:\data2 respectively. But only copy folders (and their contents) if the folder has a creation time of last month, regardless of what month that was or how many days were in the month. Also regardless of the creation time of the contents in that folder created last month. I can get the folders copied, that’s the easy part. But adding the date requirement has got me stumped. Here is my code for copying all folders and files from one drive to another.

using System;
using System.IO;

namespace DirFileCopy10
{
    class program
    {

        private static void DeepCopy(DirectoryInfo sourceDirectory, DirectoryInfo target)
        {
            throw new NotImplementedException();
        }

        static void Main()
    {
            Console.BackgroundColor = ConsoleColor.Blue;
            Console.Clear();
        // Copy from the current directory, include subdirectories.
        DirectoryCopy("C:/data1", @"D:\data1", true);
            DirectoryCopy("C:/data2", @"D:\data2", true);
        }

    private static void DirectoryCopy(string sourceDirName, string destDirName, bool copySubDirs)
    {

        // Get the subdirectories for the specified directory.
        DirectoryInfo dir = new DirectoryInfo(sourceDirName);

        if (!dir.Exists)
        {
            throw new DirectoryNotFoundException(
                "Source directory does not exist or could not be found: "
                + sourceDirName);
        }

        DirectoryInfo[] dirs = dir.GetDirectories();
        // If the destination directory doesn't exist, create it.

        if (!Directory.Exists(destDirName))
        {
            Directory.CreateDirectory(destDirName);
        }

        // Get the files in the directory and copy them to the new location.
        FileInfo[] files = dir.GetFiles();

                foreach (FileInfo file in files)
        {
            string temppath = Path.Combine(destDirName, file.Name);
                Console.WriteLine(@"Copying {0}, {1}", destDirName, file.Name);
            file.CopyTo(temppath, false);
        }

        // If copying subdirectories, copy them and their contents to new location.
        if (copySubDirs)
        {
            foreach (DirectoryInfo subdir in dirs)
            {
                string temppath = Path.Combine(destDirName, subdir.Name);
                DirectoryCopy(subdir.FullName, temppath, copySubDirs);
            }
        }
    }
  }
}

And here is what I tried adding to the top to get the date function to apply.

public static void DeepCopy(string sourceDirectory, string targetDirectory)
        {
            // Target only parent directory when applying time stamp
            DirectoryInfo Source = new DirectoryInfo(sourceDirectory);
            DirectoryInfo Target = new DirectoryInfo(targetDirectory);



            DateTime targetYear = DateTime.Now.AddYears(-1);
            DateTime targetMonth = DateTime.Now.AddMonths(-11);
            foreach (DirectoryInfo SourceDirectory in Source.GetDirectories())
                if (SourceDirectory.CreationTime.Year == targetYear.Year)
                    if (SourceDirectory.CreationTime.Month == targetMonth.Month)


                        DeepCopy(SourceDirectory, Target);
        }

I also tried renaming the “sourceDirectory” and “targetDirectory” (in the above code) to “sourceDirName” and “destDirName” as it is called in the main code body. I am trying to do this in Visual Studio 2015 C#. Nothing seems to be working for me. Any help would be greatly appreciated. Thank you.

728x90

0 Answers Copying folders and files based on creation date