C Find sharp folders using regex

What is the most efficient way to get a list of folders from a top-level directory that matches a particular regular expression? Currently, I just recursively iterate over subfolders to see if they match the regular expression, and if so, I grab the file name using the directory path.

This search currently takes approximately 50 minutes using the current method due to the number of folders located in this directory.

private void ProcessFiles(string path, string searchPattern)
{
    string pattern = @"^(\\\\server\\folder1\\subfolder\\(MENS|WOMENS|MENS\sDROPBOX|WOMENS\sDROPBOX)\\((((COLOR\sCHIPS)|(ALL\sMENS\sCOLORS)))|((\d{4})\\(\w+)\\(FINAL\sART|FINAL\sARTWORK)\\(\d{3}))))$";
    DirectoryInfo di = new DirectoryInfo(path);
    try
    {
        Debug.WriteLine("I'm in " + di.FullName);
        if (di.Exists)
        {
            DirectoryInfo[] dirs = di.GetDirectories("*", SearchOption.TopDirectoryOnly);
            foreach (DirectoryInfo d in dirs)
            {
                string[] splitPath = d.FullName.Split('\\');


                var dirMatch = new Regex(pattern, RegexOptions.IgnoreCase);

                if (dirMatch.IsMatch(d.FullName))
                {
                    Debug.WriteLine("---Processing Directory: " + d.FullName + " ---");
                    FileInfo[] files = d.GetFiles(searchPattern, SearchOption.TopDirectoryOnly);
                    AddColor(files, splitPath);
                }
                ProcessFiles(d.FullName, searchPattern);
            }
        }


    }
    catch (Exception e)
    {

    }

}
+3
source share
3 answers

I would use something like the following, no recursion needed, let BCL do this for you:

// I didn't recount the parenetheses...
Regex re = new Regex("MENS|WOMENS|MENS\sDROPBOX|WOMENS\sDROPBOX)\\((((COLOR\sCHIPS)|(ALL\sMENS\sCOLORS)))|((\d{4})\\(\w+)\\(FINAL\sART|FINAL\sARTWORK)\\(\d{3})))");
var dirs = from dir in 
           Directory.EnumerateDirectories(dirPath, "dv_*",
           SearchOption.AllDirectories)
           where re.IsMatch(dir)
           select dir;

- 50 , , .

EDIT: . , UNC. , , .

: GetDirectories ( ) EnumerateDirectories. Microsoft :

EnumerateDirectories GetDirectories : EnumerateDirectories, ; GetDirectories, , . , , EnumerateDirectories .

: , , , ( ).

+7

imo, - interop. FindFirstFile, FindNextFile, FindClose - .

http://msdn.microsoft.com/en-us/library/aa364418%28v=vs.85%29.aspx

, , .

0

You can recursively launch additional threads in subfolders to try to use any parallel features of your system, but it is likely that most of the service data is likely disk access.

0
source

All Articles