1

I am trying to enumerate file path under a directory. This directory has lots of sub directories that even though I am looking for a specific file, the enumeration is taking a lot of time. So trying to optimize it.

Here is the structure:

MAIN -> This is where I start looking

 string path = "something main here"; Directory.EnumerateFiles(path , "grouptest.log", SearchOption.AllDirectories); 

Now within the main directory, I know where all grouptest.log exists.

main -> sampleDir1 -> textDir1 -> StoryDir -> grouptest.log -> textDir2 -> StoryDir -> grouptest.log sampleDir2 -> textDir1 -> StoryDir -> grouptest.log 

It always exists two levels down. Under Main\SampleDirX\textDirX\grouptest.log. And it doesnt exist under StoryDir at all, so we dont have to look here.

I didnt find any way to exclude a pattern from the EnumerateFiles() or GetFiles() and there is no way to use regex within the search pattern.

Approaches tried:

  1. Direct enumeration of grouptest.log.
  2. Get subdirectories of main with

    Directory.GetDirectories(path, "*", SearchOption.AllDirectories).Where(dir => !Path.GetFullPath(dir).Contains(@"\StoryDir")).ToList(); 

And then read for each subDir

Directory.EnumerateFiles(subDir , "grouptest.log", SearchOption.TopDirectoryOnly); 

Still taking a long time.

How can I specify I want to exclude the directory to read before I enumerate files?

EnumerateFiles and running where clause on the file paths is still taking long time. I want to be able to exclude the StoryDir paths in the first place or just look two levels down.

1 Answer 1

1

GetDirectories with SearchOptions.AllDirectories will always scan the complete filestructure and then filter all paths with StoryDir. If you only scan on the root Directory (two times to get to the textdir), you can avoid scanning the StoryDirs:

var list = Directory.EnumerateDirectories(path, "*", SearchOption.TopDirectoryOnly). SelectMany(sampleDir => Directory.EnumerateDirectories(sampleDir, "*", SearchOption.TopDirectoryOnly)). SelectMany(textdir => Directory.EnumerateFiles(textdir, "grouptest.log", SearchOption.TopDirectoryOnly)); 
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. Just after I posted I saw this approach on this thread too. This was way too quick :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.