2019年7月15日 星期一

Copy all files and directories recursively

You can just be recursive. I just put this together - I might not have analysed your variables perfectly but it will give you an idea.
 private void btn_submit_Click(object sender, EventArgs e)
{
    copy_stuff(txt_src.Text, txt_dest.Text);
}

private void copy_stuff(string srcFolder, string destFolder)
{
    foreach (string zzz in Directory.GetFiles(srcFolder, "*.zzz", SearchOption.AllDirectories))
    {
        string modulePath = Directory.GetParent(zzz).FullName;
        string moduleName = Directory.GetParent(zzz).Name;
        Directory.CreateDirectory(destFolder + "\\" + moduleName);
        foreach (string subFolders in Directory.GetDirectories(modulePath, "*", SearchOption.AllDirectories))
        {
            string dest = subFolders.Replace(modulePath, destFolder + "\\" + moduleName);
            Directory.CreateDirectory(dest);
            copy_stuff(subfolders, dest);
        }
        foreach (string allFiles in Directory.GetFiles(modulePath, "*.*", SearchOption.AllDirectories))
        {
            File.Copy(allFiles, allFiles.Replace(modulePath, destFolder + "\\" + moduleName), true);
        }
    }
}

from : https://stackoverflow.com/questions/15577291/copy-all-files-and-directories-recursively

沒有留言:

張貼留言