Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
after checking for folders that have changed "including their content" in the last 24 hours, if there are any changes, how do I copy them from one source folder to another destination folder? the code is as below but I couldn't add 24 hours :( thank you very much in advance


What I have tried:

static void Main(string[] args)
{
    const string Src_FOLDER = @"C:\1";
    const string Dest_FOLDER = @"C:\2";
    string[] originalFiles = Directory.GetFiles(Src_FOLDER, "*", SearchOption.AllDirectories);

    Array.ForEach(originalFiles, (originalFileLocation) =>
    {
        FileInfo originalFile = new FileInfo(originalFileLocation);
        FileInfo destFile = new FileInfo(originalFileLocation.Replace(Src_FOLDER, Dest_FOLDER));

        if (destFile.Exists)
        {
            if (originalFile.Length > destFile.Length)
            {
                originalFile.CopyTo(destFile.FullName, true);
            }
        }
        else
        {
            Directory.CreateDirectory(destFile.DirectoryName);
            originalFile.CopyTo(destFile.FullName, false);
        }
    });
}
Posted
Updated 2-Jan-23 22:04pm

1 solution

You want to use the FileWatcher[^] class.

Check out this Google search for examples: filewatcher c# example[^]

UPDATE

Your question was [how to] "check for folders that have changed "including their content" in the last 24 hours".

You can "monitor" the directories that you want to watch. That is what the FileWatcher[^] class does. You can then move them as they change.

Alternatively, you can record what files are in a directory with a timestamp when checked, then after a period of 24 hours, repeat the process and compare. Then you know which files that you need to "copy them from one source folder to another destination folder".

So you have 2 choices:
1. monitor and copy as they change (FileWatcher); or
2. record, compare, move.

I'll leave the implementation for you.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900