How to also copy subfolder's new and modifed files?

ken76

Member
Joined
Nov 15, 2018
Messages
23
Programming Experience
5-10
This code only copies new and modified files under E:\Document but leaves all the subfolders and their new and modified files uncopied.
How do I also get the subfolder's new and modified files also copied?

C#:
const string sourcePath = @"E:\Document";
   const string destPath = @"D:\Test";
   string[] originalFiles = Directory.GetFiles(sourcePath, "*", SearchOption.AllDirectories);
  
   Array.ForEach(originalFiles, (originalFileLocation) =>  {
         FileInfo originalFile = new FileInfo(originalFileLocation);
         FileInfo destFile = new FileInfo(originalFileLocation.Replace(sourcePath, destPath));
         if (destFile.Exists){
              if (originalFile.Length > destFile.Length{
                    originalFile.CopyTo(destFile.FullName, true);
              }
         }
         else {
              Directory.CreateDirectory(destFile.DirectoryName);
              originalFile.CopyTo(destFile.FullName, false);
}
 
Your code above works for me except in the cases when the modified file is the same size or smaller. I suggest checking time stamps instead of file sizes.
 
Back
Top Bottom