question

KenEkholm-3624 avatar image
0 Votes"
KenEkholm-3624 asked Viorel-1 edited

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?

   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);
         }



dotnet-csharp
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

Viorel-1 avatar image
1 Vote"
Viorel-1 answered Viorel-1 edited

Instead of comparing the length with '>', try a different condition:

 if( originalFile.Length != destFile.Length || originalFile.LastWriteTime != destFile.LastWriteTime)
    . . .

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.