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?
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:
Then:
This way, once you either find a “target”, or run into a hidden directory, you stop going through sub directories.