Search code examples
c#system.io.file

Copy Directory from Source to Destination depending upon selection


I have implemented CopyDirectoryAndFiles functionality as below but it does not work correctly

Case - I want to copy/create NAC -> NAC11 -> NAC12 (This is folder structure) from source to destination it only create/copy NAC11->NAC12 I am passing source path as - @"C:\TestArea\Destination\SUP000001\20041202-01-0002-001\NAC last part of it (\NAC) is chosen at run time from UI which folder you want to copy , it can be multiple folders as well, if i only pass path as @"C:\TestArea\Destination\SUP000001\20041202-01-0002-001 it copies everything from src->dest, but i want only selected one..how i can handle this situtaion in below code ?

Main() -

    copyDirectory(@"C:\TestArea\Destination\SUP000001\20041202-01-0002-001\NAC"
,                 @"C:\TestArea\Destination\SUP000005\20150622-1205-0015-M");

Method - 

    private static void copyDirectory(string Source, string destination)
            {
                string[] Files = null;           

                if (destination[destination.Length - 1] != Path.DirectorySeparatorChar)
                {
                    destination += Path.DirectorySeparatorChar;
                }

                if (!Directory.Exists(destination))
                {
                    Directory.CreateDirectory(destination);
                }

                Files = Directory.GetFileSystemEntries(Source);
                foreach (string Element in Files)
                {
                    // Sub directories
                    if (Directory.Exists(Element))
                    {
                        copyDirectory(Element, destination  + Path.GetFileName(Element));
                    }
                    else
                    {
                        // Files in directory
                        File.Copy(Element, destination + Path.GetFileName(Element), true);
                    }
                }

            }

Solution

  • If I understand you correctly, you have the following source and destination directories specified:

    var source = @"C:\TestArea\Destination\SUP000001\20041202 - 01 - 0002 - 001\NAC";
    var dest = @"C:\TestArea\Destination\SUP000001\20041202-01-0002-001";
    

    And you want to copy the NAC directory (along with all it's contents) from source to the destination directory, but the problem is that the contents of the NAC directory are getting copied directly into the 20041202-01-0002-001 folder, when you really want to end up with a path like the following (and this NAC directory should be identical to the source NAC directory):

    @"C:\TestArea\Destination\SUP000001\20041202-01-0002-001\NAC"
    

    If that's the case, then you just need to first modify the destination directory path by adding the name of the source directory to it, like so:

    destination = Path.Combine(destination, sourceDir.Name);
    

    Here's a code sample that should do the trick:

    private static void CopyDirectory(string source, string destination)
    {
        var sourceDir = new DirectoryInfo(source);
        if (!sourceDir.Exists) throw new DirectoryNotFoundException(nameof(source));
    
        // Add last directory of source to destination and create it
        destination = Path.Combine(destination, sourceDir.Name);
        Directory.CreateDirectory(destination);
    
        // Copy files from source to destination
        foreach (var file in sourceDir.GetFiles())
        {
            file.CopyTo(Path.Combine(destination, file.Name));
        }
    
        // Recursively copy sub directories from source to destination
        foreach (var subDir in sourceDir.GetDirectories())
        {
            CopyDirectory(subDir.FullName, destination);
        }
    }