解析使用通配符的规范路径 [英] Parsing an canonical path with wildcards

查看:230
本文介绍了解析使用通配符的规范路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写它检索匹配模式的一些文件中的C#功能



输入:C:\abc * \abc \testfile? *结果
输出:所有文件匹配



我想我可以通过递归做到这一点。但它是不容易的:(



你有一个很好的算法



更新:结果
我做了基伦谢谢。)

 无效PrintAllFiles(DirectoryInfo的currentDir,
串currentPattern,串nextPatten)
{
DirectoryInfo的[] = DIS currentDir.GetDirectories(currentPattern);

如果(dis.Length大于0)
{
的String [] = remainPattern nextPatten.Split(\\.ToCharArray());
如果(remainPattern.Length大于0)
{
的foreach(在DIS DirectoryInfo的二)
{
PrintAllFiles(二,remainPattern.First(),
的string.join(\\,remainPattern.Skip(1).ToArray()));
}
}
}

的FileInfo [] = FIS currentDir.GetFiles(currentPattern);
的foreach(FileInfo的网络连接FIS)
{
Console.WriteLine(fi.DirectoryName +\\+ fi.Name);
}
}


解决方案

的最简单的就是使用递归;你会首先得到基本文件夹(C:\),然后通过 C:\ 作为当前路径, ABC * 到当前模式参数, ABC?\testfile。* 到下一个模式参数。



如果该方法找到了一个匹配的文件夹,比如C:\abc123:它会再次调用该方法,用 C:\abc123 作为当前路径, ABC?作为当前的模式',而 testfile的。* 作为下一个模式。



这是你有没有更多模式的时间来搭配,你可以停止递归和申报成功:)



希望没有什么帮助。


I am writing the C# function which retrieves some files matched pattern.

Input : C:\abc*\abc?\testfile.*
Output : All files matched.

I thought I could make it by recursion. But it was not easy :(

Do you have a nice algorithm?

Update:
I made it. Thanks Kieren :)

void PrintAllFiles(DirectoryInfo currentDir, 
                   string currentPattern, string nextPatten)
{
  DirectoryInfo[] dis = currentDir.GetDirectories(currentPattern);

  if (dis.Length > 0)
  {
    string[] remainPattern = nextPatten.Split("\\".ToCharArray());
    if (remainPattern.Length > 0)
    {
      foreach (DirectoryInfo di in dis)
      {
        PrintAllFiles(di, remainPattern.First(), 
                       string.Join("\\", remainPattern.Skip(1).ToArray()));
      }
    }
  }

  FileInfo[] fis = currentDir.GetFiles(currentPattern);
  foreach (FileInfo fi in fis)
  {
    Console.WriteLine(fi.DirectoryName + "\\" + fi.Name);
  }
}

解决方案

The easiest is by using recursion; you would get the base folder first (C:\) then pass C:\ as the current path, abc* to the 'current pattern' parameter, and abc?\testfile.* to the 'next patterns' parameter.

If that method found a folder matching, say 'c:\abc123': it would call the method again, with C:\abc123 as the current path, abc? as the 'current pattern', and testfile.* as the 'next patterns'.

By the time you have no more patterns to match, you can stop the recursion and declare success :)

Hope that helps.

这篇关于解析使用通配符的规范路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆