views:

34

answers:

2

I have a C# app that uses the search functions to find all files in a directory, then shows them in a list. I need to be able to filter the files based on extension (possible using the search function) and directory (eg, block any in the "test" or "debug" directories from showing up).

My current code is something like:

Regex filter = new Regex(@"^docs\(?!debug\)(?'display'.*)\.(txt|rtf)");
String[] filelist = Directory.GetFiles("docs\\", "*", SearchOption.AllDirectories);
foreach ( String file in filelist )
{
    Match m = filter.Match(file);
    if ( m.Success )
    {
        listControl.Items.Add(m.Groups["display"]);
    }
}

(that's somewhat simplified and consolidated, the actual regex is created from a string read from a file and I do more error checking in between.)

I need to be able to pick out a section (usually a relative path and filename) to be used as the display name, while ignoring any files with a particular foldername as a section of their path. For example, for these files, only ones with +s should match:

+ docs\info.txt
- docs\data.dat
- docs\debug\info.txt
+ docs\world\info.txt
+ docs\world\pictures.rtf
- docs\world\debug\symbols.rtf

My regex works for most of those, except I'm not sure how to make it fail on the last file. Any suggestions on how to make this work?

+2  A: 

Try Directory.GetFiles. This should do what you want.

Example:

// Only get files that end in ".txt"
string[] dirs = Directory.GetFiles(@"c:\", "*.txt", SearchOption.AllDirectories);
Console.WriteLine("The number of files ending with .txt is {0}.", dirs.Length);
foreach (string dir in dirs) 
{
   Console.WriteLine(dir);
}
SwDevMan81
That's the function I'm using, actually (typo in the code example). My problem is it won't filter out files that are in a "bad" subdirectory within that directory. I also need to keep the filter in a file (though that can be done with GetFiles too).
peachykeen
A: 
^docs\\(?:(?!\bdebug\\).)*\.(?:txt|rtf)$

will match a string that

  1. starts with docs\,
  2. does not contain debug\ anywhere (the \b anchor ensures that we match debug as an entire word), and
  3. ends with .txt or .rtf.
Tim Pietzcker