使用通配符复制文件夹中的文件(文件夹路径具有通配符)

时间:2015-12-18 10:53:20

标签: c# .net windows winforms windows-applications

美好的一天。

我必须将文件夹(包括子文件夹)中的所有文件复制到其他共享驱动器位置以备份数据。我面临的挑战是带有通配符的文件夹路径。

例如,

文件夹结构如下所示

  

d:/文件夹/ Folder11 / Folder111

     

d:/ FOLDER2 / Folder222 / Folder222222

     

d:/ Folder3 / Folder333333 / Folder3333333

我正在寻找的输入格式应该是“D:/ Folder?/ Folder * / Folder *”。因此必须根据通配符模式进行循环。

你能帮我吗?

此致

钱德拉

2 个答案:

答案 0 :(得分:0)

您可以使用简单的RegularExpression来实现此目的。我已经创建了一个为您完成工作的示例。

RegEx字符串非常简单:[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

请参阅regexr处的示例。

修改

//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;
    }
}

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

答案 1 :(得分:-1)

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

然后此调用将返回与您的模式匹配的所有文件夹的列表:

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