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)
{
}
}
Jesse source
share