1

In a .NET code, I have a loop that takes all .wav files from D:\Temp and its subdirectories :

string[] fileHolder = Directory.GetFiles("D:\\Temp","*.wav", SearchOption.AllDirectories);

I want to exclude all .wav files coming from folders that have "blablabla" in their folder name.

How do do this ?

P.S. This is similar to Exclude directories that contain sub directories?, but I haven't been able to use successfully the answers of this topic.

Community
  • 1
  • 1
Basj
  • 41,386
  • 99
  • 383
  • 673
  • 2
    It looks like everything's there in the linked question. What's exactly the problem with applying the answer to what you're trying to achieve? – dtb Oct 20 '12 at 20:36

3 Answers3

10
IEnumerable<string> files = 
   from f in Directory.GetFiles("D:\\Temp", "*.wav", SearchOption.AllDirectories)
   where !Path.GetDirectoryName(f).Contains("blablabla")
   select f;

Adding several conditions:

var files = from f in Directory.GetFiles("D:\\Temp", "*.wav", SearchOption.AllDirectories)
            let directoryName = Path.GetDirectoryName(f)
            where !directoryName.Contains("blablabla") &&
                  !direcotyName.Contains("foo")
            select f;

Another solution: (I think that Path.GetDirectoryName is not fastest possible operation)

public static IEnumerable<string> GetFiles(string path, string searchPattern,
                                           Func<string, bool> dirSelector)
{
    if (!dirSelector(path))
        yield break;

    foreach (var file in Directory.GetFiles(path, searchPattern))
        yield return file;

    foreach (var dir in Directory.GetDirectories(path))
        foreach (var file in GetFiles(dir, searchPattern, dirSelector))
            yield return file;
}

Usage:

Func<string, bool> dirSelector = (d) => !d.Contains("blablabla");
string[] files = GetFiles("D:\\Temp", "*.wav", dirSelector).ToArray();
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
3

Try following:

string[] fileHolder = Directory.GetFiles(@"d:\temp", "*.wav",
         SearchOption.AllDirectories).Where(file => !file.Contains("blabla")).ToArray();
Michal Klouda
  • 14,263
  • 7
  • 53
  • 77
  • +1 but if the searched text is in the filename and not in the folder name, the file will be excluded – Steve Oct 20 '12 at 20:40
  • I think this query will also filter files, which have `blabla` in their names. – Sergey Berezovskiy Oct 20 '12 at 20:40
  • you're both right, thanks.. `Path.GetDirectoryName` or `Regex` would be needed then.. depends on situation – Michal Klouda Oct 20 '12 at 20:42
  • Is this correct then (if I want to filter foldername only) : `string[] fileHolder = Directory.GetFiles(@"d:\temp", "*.wav", SearchOption.AllDirectories).Where(file => !Path.GetDirectoryName(file).Contains("blabla")).ToArray();` – Basj Oct 20 '12 at 20:50
  • last question : how can I exlcude files, with foldername containing "blablabla1" or "blablabla2" or "blablabla3" (I only have 3 names to exclude) – Basj Oct 20 '12 at 20:55
1

You will have to sort them out manually:

List<string> result = new List<string>();
foreach(string filename in Directory.GetFiles("D:\\Temp", "*.wav"))
{
   if (!filename.Contains("blablabla"))
      result.Add(filename);
}
Casperah
  • 4,504
  • 1
  • 19
  • 13