查找系统中位于定义的文件夹层次结构内的所有文件 [英] Find all files in system that are located inside defined hierarchy of folders

查看:174
本文介绍了查找系统中位于定义的文件夹层次结构内的所有文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在系统中找到名称为 TestName.lol 的文件,位于 TestRoot\TestSubFoler\Test\

I'd like to find all files in system with name TestName.lol that lie inside TestRoot\TestSubFoler\Test\.

如果在磁盘 C D 上有 TestRoot\TestSubFoler\Test\TestName.lol ,则我想获得2个文件。

I.e. if a have TestRoot\TestSubFoler\Test\TestName.lol both on disk C and D, then I'd like to obtain 2 files.

我考虑过不同的方法:


  1. C:\ D:\ 开始,然后递归搜索 TestRoot 。然后对所有发现的事件执行相同操作,而不是 C:\ D:\ 。等等。

  1. start with C:\ and D:\ and then recursively search for TestRoot. Then do the same for all found occurances instead of C:\ and D:\. And so on.

递归搜索 TestName.lol 的所有文件夹。然后过滤掉位于错误文件夹中的那些。 (我相信在我的情况下,这将更快,因为这样的文件的估计数量不大:1-10)

recursively search all folders for TestName.lol. Then filter out those located in 'wrong' folders. (I believe that in my case this will be faster, as the estimated number of such files is not large: 1-10)

我相信可以有一个更好的方式(更优雅或更好的表现)。无论如何,如果您认为我的解决方案可行,只需确认。

??? I believe there can be a better way to this (more elegant or with better performance). Anyway, if you think my solution is OK, just confirm pls.

谢谢。

编辑:
目录中的搜索文件使用复杂的模式一个类似的问题。

Search file in directory using complex pattern a similar question.

推荐答案

我个人觉得很难避免递归。因为文件系统没有索引。 Google Desktop或Microsoft Desktop搜索索引所有文件。那么如果你询问你会很快得到答案。

I personally feel it is difficult to avoid recursion . Because the file system is not Indexed. Google Desktop or Microsoft Desktop search indexed all files. there if you query you will get the answer fairly very quick.

我们选择的是.net框架,为你做递归,或者你自己做。

The choice we have is .net framework does recursion for you or you do it yourself.

其他选项是Linq - 我猜,.NET框架会做递归。但是它会更清洁

Other option is Linq - which i guess .net framework will do the recursion . but it will be cleaner

Linq

http://msdn.microsoft.com/en-us/library/bb882649.aspx

// Take a snapshot of the file system.
        System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(startFolder);

        // This method assumes that the application has discovery permissions 
        // for all folders under the specified path.
        IEnumerable<System.IO.FileInfo> fileList = dir.GetFiles("*.*", System.IO.SearchOption.AllDirectories);

        string searchTerm = @"Visual Studio";

        // Search the contents of each file. 
        // A regular expression created with the RegEx class 
        // could be used instead of the Contains method. 
        // queryMatchingFiles is an IEnumerable<string>. 
        var queryMatchingFiles =
            from file in fileList
            where file.Extension == ".htm" 
            let fileText = GetFileText(file.FullName)
            where fileText.Contains(searchTerm)
            select file.FullName;

        // Execute the query.
        Console.WriteLine("The term \"{0}\" was found in:", searchTerm);
        foreach (string filename in queryMatchingFiles)
        {
            Console.WriteLine(filename);
        }

.net代码

foreach (FileInfo fi in directory.GetFiles())
{
  // Console.WriteLine(@"Found file: [{0}] in directory: [{1}]", fi.Name, directory.FullName);

}
 foreach (DirectoryInfo diSourceSubDir in directory.GetDirectories())
 {
    // Console.WriteLine(@"Sub Folder {0} found.", diSourceSubDir.FullName);
 }

这篇关于查找系统中位于定义的文件夹层次结构内的所有文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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