-1

I want to copy some of the content of a folder (recursively). only files that contain a specific pattern.

Here there's a method that copies the entire content. no matter what files inside.

https://stackoverflow.com/a/3822913/7028967

Do you have any ideas on how can I copy a specific file inside some subfolder with a given pattern?

for example:

-- rootFolder
---- filename.txt

CopyContent(src, dest, *.txt)

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
someone
  • 29
  • 3
  • `"*.*"` in `GetFiles()` is the filter. – Jimi May 10 '21 at 16:44
  • The answer you linked to is the answer to your question. Just change the `*.*` in the call to `Directory.GetFiles` to `*.txt` – McAden May 10 '21 at 16:51
  • Welcome to SO! Please read the [how-to-ask page](https://stackoverflow.com/help/how-to-ask) and read [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/minimal-reproducible-example) to improve your question and help us to understand your problem. – Trevor May 10 '21 at 16:54
  • 3
    Welcome to Stackover, I would suggest you to attempt this yourself and put your work into the question. Google can help, also searching for examples. It's important to have a go anyway as this helps with your learning and also your understanding. – Neil May 10 '21 at 17:06

2 Answers2

0

Below sample code to copy all the files from the source directory to destination folder in exact same folder structure :

       public static void Copy()
        {
            string sourceDir = @"C:\test\source\";
            string destination = @"C:\test\destination\";

            string[] textFiles = Directory.GetFiles(sourceDir, "*.txt", SearchOption.AllDirectories);

            foreach (string textFile in textFiles)
            {
                string fileName = textFile.Substring(sourceDir.Length);
                string directoryPath = Path.Combine(destination, Path.GetDirectoryName(fileName));
                if (!Directory.Exists(directoryPath))
                    Directory.CreateDirectory(directoryPath);

                File.Copy(textFile, Path.Combine(directoryPath, Path.GetFileName(textFile)), true);
            }
        }

Updated:

enter image description here

Rahul Shukla
  • 646
  • 6
  • 19
  • IMHO I would use the [Directory.GetFiles(string path, string searchPattern, System.IO.SearchOption searchOption)](https://learn.microsoft.com/en-us/dotnet/api/system.io.directory.getfiles?view=net-5.0#System_IO_Directory_GetFiles_System_String_System_String_System_IO_SearchOption_) which you can provide a search option to include all sub directories. By default, the one you're using does `SearchOption.TopDirectoryOnly` and not sub directories. – Trevor May 10 '21 at 17:29
  • Hey. Thank you. but it doesn't work for subdirectories content. let's assume I have -- root folder ---- subfolder ------ filename.txt I want to copy the subfolder and filename.txt to the destination. any suggestions? – someone May 10 '21 at 18:17
  • @someone I have update the code with the debug screenshot, it working solution. Accept ans if it solves your problem – Rahul Shukla May 10 '21 at 18:49
-1

I used this solution. and it works for me!

private static void CopyContentFolder(string sourcePath, string targetPath, bool isFileOverridable, string pattern = "*.*")

{
    string[] sourceFiles = Directory.GetFiles(sourcePath, pattern, SearchOption.AllDirectories);

    foreach (string sourceFilePath in sourceFiles)
    {   
        string destinationFilePath = string.Empty;
        destinationFilePath = sourceFilePath.Replace(sourcePath, targetPath);
        EvaluatePath(destinationFilePath);
        File.Copy(sourceFilePath, destinationFilePath, isFileOverridable);
    }
}

private static void EvaluatePath(string path)
{
    try
    {
        string folder = Path.GetDirectoryName(path);
        if (!Directory.Exists(folder))
        {
            DirectoryInfo di = Directory.CreateDirectory(folder);
        }
    }
    catch (IOException ioex)
    {
        Console.WriteLine(ioex.Message);
    }
}
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
someone
  • 29
  • 3