Chandra
Chandra

Reputation: 31

Copy the files from folders recursively with wildcard characters (folder path has wildcard characters)

Good day.

I have to copy all the files in folders (including sub folders) to other shared drive location for backup the data. The challenge, which I am facing is folder path with wildcard characters.

For example,

The folder structure is like below

D:/Folder1/Folder11/Folder111

D:/Folder2/Folder222/Folder222222

D:/Folder3/Folder333333/Folder3333333

I am looking for the input format should be "D:/Folder?/Folder*/Folder*". So that it has to loop according to the wildcard character patterns.

Can you please help me.

Regards,

Chandra

Upvotes: 2

Views: 892

Answers (2)

srandppl
srandppl

Reputation: 571

IEnumerable<string> getMatchingSubDir(string dirPath, string pattern)
{
   List<string> matchingFolders = new List<string>();
   DirectoryInfo myDir = new DirectoryInfo(dirPath);
   foreach (var subDir in myDir.GetDirectories(pattern))
   {
       matchingFolders.AddRange(getMatchingSubDir(subDir.FullName, pattern));
   }
   return matchingFolders;
}

Then this call will return you a list of all folders matching your pattern:

getMatchingSubDir("D:\\", "Folder*");

Upvotes: -1

cramopy
cramopy

Reputation: 3497

You can achive this with a simple RegularExpression. I've created an example which does the job for you.

The RegEx string is quite easy: [A-Z]:\\Folder[0-9]{1}\\Folder[0-9]{2}\\Folder[0-9]{3}

[A-Z]:\\Folder[0-9]{1}\\Folder[0-9]{2}\\Folder[0-9]{3}
-----         --------        --------        --------
Drive         1x digit        2x digit        3x digit

See the sample at regexr.

EDIT:

//using System.IO;

public void CopyMatching(string drive)
{
    try
    {
        var backuplocation = ""; //the path where you wanna copy your files to
        var regex = new Regex(@"[A-Z]:\\Folder[0-9]{1}\\Folder[0-9]{2}\\Folder[0-9]{3}");
        var directories = new List<string>();
        foreach (var directory in Directory.EnumerateDirectories(drive))
        {
            if (regex.IsMatch(directory))
            {
                directories.Add(directory);
            }
        }
        foreach (var directory in directories)
        {
            DirectoryCopy(directory, backuplocation, true);
        }
    }
    catch (Exception)
    {
        throw;
    }
}

And DirectoryCopy:

public 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);
        }
    }
}

Upvotes: 0

Related Questions