Mike Schiraldi
Mike Schiraldi

Reputation: 35

c# searching using directory class

I am writing a software tool which, as part of its main task, must search a directory and its sub directories for a directory with a given name, and save to a string array each file path that terminates with the specified directory name. For example:

                   level_1      level_2      Level_3
RootDirectory ---> folderA ---> folderD ---> FolderF ---> Target
             |            |---> folderE ---> Target
             |            
             |---> folderB ---> Target
             |
             |---> FolderC ---> Target

should pump out:

string[] = {RootDirectory\folderA\FolderD\folderF\Target,  
    RootDirectory\folderA\folderE\Target, 
    Rootdirectory\folderB\Target, 
    RootDirectory\foderC\Target}

Originally I used getDirectories(myPath, "Target", SearchOption.AllDirectories) on a directory info object, but there was an issue. For some reason, it would find the target under folders b and c, and also under folderA>folderD>folderF, but would skip FolderE. Once it found the first occurrence within the sub directory, folderA, it would go on to the next folder at level_1. I should mention that folderD in my real-case was in fact alphabetically sorted before folderE, as it is in this example

so instead I decided to use an IEnumerator and run a where filter to select the files that terminate with the given directory name. This found them all. However, i cannot figure out how to do something like getDirectories().Where(x=>(x.attributes & fileattributes.hidden)==0); on an IEnumerator.

The problem is, I need it to skip hidden SVN directories because it is slowing down the process considerably.

So here is my question: how can I get a collection of all paths within a sub directory that end in a given directory name, and exclude hidden files form the search?

Upvotes: 3

Views: 497

Answers (2)

Eric Dahlvang
Eric Dahlvang

Reputation: 8292

I think you're going to have to write your own implementation. If you are in .net 4.0, you could use EnumerateDirectories

and do something like this:

private IEnumerable<DirectoryInfo> EnumerateDirectories(DirectoryInfo dir, string target)
{
    foreach (var di in dir.EnumerateDirectories("*",SearchOption.TopDirectoryOnly))
    {
        if ((di.Attributes & FileAttributes.Hidden) != FileAttributes.Hidden)
        {
            if (di.Name.EndsWith(target, StringComparison.OrdinalIgnoreCase))
            {
                 yield return di;
                 continue;
            }
            foreach (var subDir in EnumerateDirectories(di, target))
            {
                yield return subDir;
            }
        }
    }
}

Then:

DirectoryInfo dir = new DirectoryInfo(@"C:\RootDirectory");
var found = EnumerateDirectories(dir,"target").ToArray();

This way, once you either find a "target", or run into a hidden directory, you stop going through sub directories.

Upvotes: 0

dplante
dplante

Reputation: 2449

I created your folder structure in the root of my C:\ and came up with the following code using Linqpad.

string root = "c:\\folderA";
string target = "Target";   
var d = new DirectoryInfo(root);
var x = d.EnumerateDirectories(target, SearchOption.AllDirectories);
x.ToList() // for each element, it's in the the FullName 

The only part missing is ignoring the .svn folders

Upvotes: 1

Related Questions