0

VB c# beginner here. I am using the following snippet of code to list out all the videos in a directory.

    var files = Directory
        .GetFiles("D:\\Movies", "*.*", SearchOption.AllDirectories)
        .Where(s => s.ToLower().EndsWith(".mp4") || s.ToLower().EndsWith(".mkv") || s.ToLower().EndsWith(".avi"));

Now the challenge is to exclude those videos which have 'sample' word in their names.

I tried adding .Where(s => s != "sample.avi"); at the end but it doesn't seem to work.

also when I do .Where(s => s.Contains("sample");, I am able to list out all the files which have the word 'sample' in their names.

Also is it possible to delete those names from the files array or enum whatever that is ? I don't think that works as an array because finding the count doesn't work like files.Count / files.Length.

I think my understanding of enums and string is a little weak here. Please guide.

aelor
  • 10,892
  • 3
  • 32
  • 48

3 Answers3

0

The second parameter to the method .GetFiles() is the search pattern you need not to perform anymore filtering if you specifying the search pattern here: you will get all files with name sample if you give the pattern as: "sample.*" or you will get all files whose name ends with sample but having any extension by using "*Sample.*" So use like this:

 string pattern="sample.avi";
 var files = Directory
        .GetFiles("D:\\Movies", pattern, SearchOption.AllDirectories);

If you want to use multiple filters means use like this:

string pattern="sample.*";
var files = Directory
            .GetFiles("D:\\Movies", pattern, SearchOption.AllDirectories)
            .Where(s => s.ToLower().EndsWith(".mp4") || 
            s.EndsWith(".mkv",StringComparison.InvariantCultureIgnoreCase)||
            s.EndsWith(".avi",StringComparison.InvariantCultureIgnoreCase));

So the .GetFiles() will give you all the files having name as sample the Where will filter files with extension .mkv or .avi or mp4

sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
  • this will only fetch me files with names sample.avi, What I want to achieve is all the files which do not contain the word 'sample' in their names. – aelor Jun 08 '16 at 01:05
  • Using `ToLower` will create a bunch of unnecessary strings. Use the overload of `EndsWith` that allow you to specify a case insensitive search: `s.EndsWith(".mkv", StringComparison.InvariantCultureIgnoreCase)` – Chris Dunaway Jun 08 '16 at 13:54
  • @ChrisDunaway: thanks for the advise, it helps me to improve my answer – sujith karivelil Jun 08 '16 at 14:01
0

You may be looking for String.IndexOf(string)

https://msdn.microsoft.com/en-us/library/k8b1470s(v=vs.110).aspx

Returns -1 if not found. If any other value is returned, name contains the argument string.

Whazz
  • 367
  • 2
  • 12
0

Thanks for your help guys, got it to work by using

.Where(s => !s.ToLower().Contains("sample"));

This excludes the names which have 'sample' word in them.

aelor
  • 10,892
  • 3
  • 32
  • 48
  • Don't use `ToLower`. You are just creating a bunch of unnecessary strings. Use the overload of `Contains` that allow you to specify a case insensitive search: `.Where(s => !s.Contains("sample", StringComparer.InvariantCultureIgnoreCase));` – Chris Dunaway Jun 08 '16 at 13:52