Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 430433
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T19:52:30+00:00 2026-05-12T19:52:30+00:00

I need to perform some operations on all folders within a file share which

  • 0

I need to perform some operations on all folders within a file share which match a defined pattern. The pattern may refer to a number of levels in the tree, e.g.
\Test\[a-z]+\Project[0-9]{3}

What is the most efficient way to traverse the tree to find all matching folders? Is there a better way to do this than a simple recursive depth first search using DirectoryInfo and di.GetDirectories(), like such:

private void TraverseSubFolder(DirectoryInfo folder)
{
    if (filter.IsMatch(folder.FullName)) {
       DoStuff(folder);
    }

    DirectoryInfo[] subFolders = folder.GetDirectories();
    foreach (DirectoryInfo sf in subFolders)
    {
        TraverseSubFolder(sf);
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T19:52:31+00:00Added an answer on May 12, 2026 at 7:52 pm

    You could use Linq to filter

    Regex regex = new Regex("your regex");
    var directories = Directory.GetDirectories("c:\\", null, SearchOption.AllDirectories).Where(directory => regex.IsMatch(directory));
    

    The drawback of this approach is that it will still search into unwanted folder that were filtered out since the Where occurs after all folders are returned.

    This could be adapted.

    Edit

    This will not work with SearchOption.AllDirectories since as soon as you hit a folder where you have no right, UnauthorizedAccessException will be thrown.

    I don’t think you can go without a recursive function because of the check for UnauthorizedAccessException.

    I coded this approach using Linq, but it is not very different from your own approach. At least it check for permission. It is still prone to a StackOverflowException.

    private static void Traverse(List<string> folders, string rootFolder, Regex filter)
    {
        try
        {
            // Test for UnauthorizedAccessException
            new FileIOPermission(FileIOPermissionAccess.PathDiscovery, rootFolder).Demand();
    
            Array.ForEach(Directory.GetDirectories(rootFolder),
                (directory) =>
                {
                    if (filter.IsMatch(directory))
                    {
                        folders.Add(directory);
    
                        Traverse(folders, directory, filter);
                    }
                });
        }
        catch 
        {
            // Ignore folder that we don't have access to
        }
    }
    
    // Usage example
    List<string> folders = new List<string>();
    Regex regex = new Regex("^.+$");
    Traverse(folders, "e:\\projects", regex);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to perform some file operations on WinCE platform. The paths of file
I am trying to catch a file sent with form and perform some operations
I need a function which takes an arbitrary number of arguments (All of the
I have a service with some entities that I would like to expose in
I'm looking for something that can interpret a string in php and perform simple
I'll start in the way most people seem to have taken to, on here....
I have a data set of books and authors, with a many-to-many relationship. There
I'm trying to support multiple databases for an app that I'm writing. The app
I am working on a project in C#.NET using the .NET framework version 3.5.
If I'm using an ORM like JPA2 - where I have my entities that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.