none
Copy folder structure to different location RRS feed

  • Question

  • Hi,

    There is a folder structure like "E:\Work_Status\Framework\Version_Changes\ZippedOutputProjsSBs\Upgrade\SB\UPG_\01_01_01\SB".

    I want to move SB folder and its contents to another location. There are other files under 01_01_01, which should not be copied to destination. Only SB folder should be copied. (name SB remains is fixed for all the files)

    In the destination location, the SB folder should be copied under its immediate parent name like D:\Destination\01_01_01\SB.

    Under UPG_ folder there are 2000+ folders like 01_01_02, 01_01_03 etc.

    I want to run this grammatically, so that all the files are copied at once.

    Kindly waiting for your response.

    Thanks,

    Santosh

    Friday, November 15, 2019 6:27 AM

All replies

  • Hi Santosh,

    There is a folder structure like "E:\Work_Status\Framework\Version_Changes\ZippedOutputProjsSBs\Upgrade\SB\UPG_\01_01_01\SB".

    I want to move SB folder and its contents to another location. There are other files under 01_01_01, which should not be copied to destination. Only SB folder should be copied. (name SB remains is fixed for all the files)

    --->Only SB folder should be copied(name SB remains is fixed for all the files)

    Do you mean to copy Dir "SB" and all files under "SB" from "e:\Src\SB" to "e:\Dest\SB"?

    Best regards,

    E-John

    using System.IO;
    using System.Linq;
    
    
    namespace DirAndFilesCopy
    {
        class Program
        {
            static void Main(string[] args)
            {
                string srcFullDirName = @"e:\Src\SB";
                string destPath = @"e:\Dest";
                string destFullDirName = null;
                
                // Get the last part of srcFullDirName, "SB"
                string srcDir = srcFullDirName.Split(Path.DirectorySeparatorChar).Last();
                // Combine "SB" with destPath to make destFullDirName
                destFullDirName = Path.Combine(destPath, srcDir);
                // Copy from the current directory, include subdirectories.
                DirectoryCopy(srcFullDirName, destFullDirName, true);
            }
    
            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);
                    }
                }
            }
        }
    }


    • Edited by E-John Friday, November 15, 2019 6:59 AM
    Friday, November 15, 2019 6:54 AM
  • Hi Santosh Umarani, 

    Thank you for posting here.

    For your question, you want to copy folder structure to different location.

    I make a test on my side, and you can refer to the following code.

            static void Main(string[] args)
            {
                string srcDirName = @"D:\TestFile\";
                string destPath = @"D:\TestFile2";
                string destFullDirName = null;
                string name = null;
                DirectoryInfo dir = new DirectoryInfo(srcDirName);
                DirectoryInfo[] dirs = dir.GetDirectories();
                string[] strArray;
                int result;
                foreach (DirectoryInfo info in dirs)
                {
                    strArray = info.Name.Split('_');
                    if (strArray.Length != 3)
                    {
                        continue;
                    }
                    else
                    {
                        if (int.TryParse(strArray[0], out result) && int.TryParse(strArray[1], out result) && int.TryParse(strArray[2], out result))
                        {
                            name = info.Name + "\\SB";
                            destFullDirName = Path.Combine(destPath, name);
                            Copy(srcDirName + name, destFullDirName);
                        }
                    }               
                }
            }
            public static void Copy(string sourceFolderPath, string destinationPath)
            {
                DirectoryInfo dir = new DirectoryInfo(sourceFolderPath);
                if (!dir.Exists)
                {
                    throw new DirectoryNotFoundException(
                        "Source directory does not exist or could not be found: "
                        + sourceFolderPath);
                }
    
                DirectoryInfo[] dirs = dir.GetDirectories();
                if (!Directory.Exists(destinationPath))
                {
                    Directory.CreateDirectory(destinationPath);
                }
                
                try
                { 
                    foreach (FileInfo f in dir.GetFiles())
                    {
                        File.Copy(f.FullName, Path.Combine(destinationPath, f.Name));
                    }
                    var s = dir.GetDirectories();
                    if (s.Length == 0)
                    {
                        return;
                    }
                    foreach (DirectoryInfo d in s)
                    {
                        string str = d.FullName + "\\";
                        Directory.CreateDirectory(destinationPath + "\\" + d.Name);
                        Copy(str, destinationPath + "\\" + d.Name);
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
            }

    Folders in my ‘TestFile’.

    Result of my test.

    Hope it can help you.

    Best Regards,

    Xingyu Zhao



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Friday, November 15, 2019 10:05 AM
    Moderator