CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 1 of 1
  1. #1
    Join Date
    Nov 2018
    Posts
    12

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

    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?

    Code:
    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);
             }
    }
    Last edited by kenne76; September 21st, 2021 at 07:43 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured