Home > other >  Iterate inside folders and copy subfolders only
Iterate inside folders and copy subfolders only

Time:01-20

I am working on a project and what I have to do is iterate inside some folders and copy only the subfolders of them and paste in in some different location.

Here's the folders structure:

-Folder1
--subfolder1
---test.doc
---test.pdf
---test.xlsx
--subfolder2
---test.mp3
---test.pdf
---test.eml

-Folder2
--subfolder4
---test2.pdf
---test2.msg
---test2.eml
--subfolder3
---test4.msg
---test4.eml
---test4.pdf

Now, what I want to do is "extract" the subfolders from their parents and copy to them to a different directory. In the end the structure has to be like this:

--subfolder1
--subfolder2
--subfolder3

(of course with their corresponding files inside them)

Here's what I have tried so far but it doesn't seem to do the exact thing that I want, it just copies all the folder how they appear in the target directory, with the parent folders too.

    static void CopyDirectory(string sourceDir, string destinationDir, bool recursive)
    {
        // Get information about the source directory
        var dir = new DirectoryInfo(sourceDir);

        // Check if the source directory exists
        if (!dir.Exists)
            throw new DirectoryNotFoundException($"Source directory not found: {dir.FullName}");

        // Cache directories before we start copying
        DirectoryInfo[] dirs = dir.GetDirectories();

        // Create the destination directory
        Directory.CreateDirectory(destinationDir);

        // Get the files in the source directory and copy to the destination directory
        foreach (FileInfo file in dir.GetFiles())
        {
            string targetFilePath = Path.Combine(destinationDir, file.Name);
            file.CopyTo(targetFilePath);
        }

        // If recursive and copying subdirectories, recursively call this method
        if (recursive)
        {
            foreach (DirectoryInfo subDir in dirs)
            {
                string newDestinationDir = Path.Combine(destinationDir, subDir.Name);
                CopyDirectory(subDir.FullName, newDestinationDir, true);
            }
        }
    }

Thank you in advance.

CodePudding user response:

In recursive function you have to create another loop which iterates inside of subfilders and then you just copy the files inside the new directory. Basically like this:

 if (recursive)
            {
                foreach (DirectoryInfo subDir in dirs)
                {
                    DirectoryInfo[] Subdirs = subDir.GetDirectories(); 
                    foreach (DirectoryInfo subDir2 in Subdirs)
                    {
                        string newDestinationDir = Path.Combine(destinationDir, subDir2.Name);
                        CopyDirectory(subDir2.FullName, newDestinationDir, true);
                        Console.WriteLine("Finnished copying file: "   subDir2.Name);
                    }
                }
            }
  •  Tags:  
  • Related