Reputation: 4753
I'd like to find all files in system with name TestName.lol
that lie inside TestRoot\TestSubFoler\Test\
.
I.e. if a have TestRoot\TestSubFoler\Test\TestName.lol
both on disk C and D, then I'd like to obtain 2 files.
I considered different ways to do this:
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.
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.
Thanks.
Edit: Search file in directory using complex pattern a similar question.
Upvotes: 0
Views: 637
Reputation: 5815
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.
The choice we have is .net framework does recursion for you or you do it yourself.
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 code
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);
}
Upvotes: 1
Reputation: 134045
Take a look at the Directory.GetFiles method. That should do what you want.
Actually, this overload is probably the one you need.
Or, you could call Directory.EnumeratFiles, which will do the recursion for you.
Your option #2 is probably the way to go.
There really isn't a particularly efficient way to do this. You can speed it up somewhat by making direct calls to the Windows FindFirst
and FindNext
API functions, but it's really not worth the effort unless you're doing this a lot.
Upvotes: 1