How to copy new added folder in Source folder to Target folder by clicking a button?

Jack Cheng

New member
Joined
Apr 15, 2019
Messages
1
Programming Experience
Beginner
Hello everyone,
I am using Visual C# and create windows form. There have only one button in interface.
There have 2 folders with name 'Source' and 'Target', in 'Source' folder contains '2019', '2018', '2017' folders. Once a day, there is 1 folder's name start with '201904" with 10 file's name start with "text" will be downloaded and move to "C:/Users/Source/2019/".

According to codes that I have for now:
In day 1, I have been click a button and then a folder "20190401" in "C:/Users/Source/2019/" copied to "C:/Users/Target/".
But in day 2, the new downloaded folder "20190402" in '"C:/Users/Source/2019/" wouldn't copy to "C:/Users/Target/" after I click a button for 2nd time, because it cannot overwrite the existed "20190401" folder in "C:/Users/Target/".
The result that I expected, "C:/Users/Target/" will collects "201904**"folder in daily by clicking button once a day. Furthermore, only [3] files in folder "201904**" name start with "text" will be copy to "C:/Users/Target/". Do you guys have any idea to solve this problem?

C#:
private void button1_Click(object sender, EventArgs e)
{
    string FROM_DIR = "C:/Users/Source/";
    string TO_DIR = "C:/Users/Target/";   
    DirectoryInfo diCopyForm = new DirectoryInfo(FROM_DIR);
    DirectoryInfo[] fiDiskfiles = diCopyForm.GetDirectories();
    string directname = "201904";
    string filename = ".txt";
    foreach (DirectoryInfo newfile in fiDiskfiles)
    {
        try
        {
            if (newfile.Name == "2019")
            {
                foreach (DirectoryInfo direc in newfile.GetDirectories())
                    if (direc.Name.StartsWith(directname))
                    {
                        int count = 0;

                        foreach (FileInfo file in direc.GetFiles())
                        {
                            if (file.Name.EndsWith(filename))
                            {
                                count++;
                            }
                        }
                        if (count == 3)
                        {
                            DirectoryCopy(direc.FullName,Path.Combine(TO_DIR,direc.Name), true);
                            count = 0;
                            MessageBox.Show("success");
                        }
                    }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
}

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

Best regards,

Jack Cheng
 

Attachments

  • Code.zip
    967 bytes · Views: 62
Last edited by a moderator:

Latest posts

Back
Top Bottom