1

For finding all .txt files, we can use this:

Directory.GetFiles(@"c:\","*.txt")

Is there any way to find all files not matching a pattern (for ex: all files not having extension .txt).

one noa
  • 345
  • 1
  • 3
  • 10
Akshay J
  • 5,362
  • 13
  • 68
  • 105

3 Answers3

5

You can try LINQ:

var files = Directory.EnumerateFiles("C:\\").Where(x => !x.EndsWith(".txt")).ToList();
Igor Ševo
  • 5,459
  • 3
  • 35
  • 80
3

No builtin way as search pattern. But you could use Linq:

var files = Directory.EnumerateFiles(dir)
  .Where(fn => !Path.GetExtension(fn).Equals(".txt", StringComparison.OrdinalIgnoreCase))
  .ToArray();

Note that i've used EnumerateFiles instead of GetFiles. The latter loads al files into memory before you can start processing, with EnumerateFiles you can start enumerating and filtering the collection of names before the whole collection is returned.

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • @HamletHakobyan It is for using `OrdinalIgnoreCase` – Sriram Sakthivel Oct 11 '13 at 09:30
  • @SriramSakthivel Ok, but my question was about another case which Tim have fixed already. – Hamlet Hakobyan Oct 11 '13 at 09:31
  • Any reason to use `EnumerateFiles`? this is anyway going to check all the files right? I think there is no benefit – Sriram Sakthivel Oct 11 '13 at 09:34
  • @SriramSakthivel: i have tried to explain it in my answer, however, msdn is clearer: "The EnumerateFiles and GetFiles methods differ as follows: When you use EnumerateFiles, you can start enumerating the collection of names before the whole collection is returned; when you use GetFiles, you must wait for the whole array of names to be returned before you can access the array. Therefore, when you are working with many files and directories, EnumerateFiles can be more efficient" – Tim Schmelter Oct 11 '13 at 09:37
  • So this will avoid overhead of loading all filenames in memory right? I don't see performance wise any difference because both does same job in this context(if we use` Take(4)` that is different). am I right? correct me if am wrong – Sriram Sakthivel Oct 11 '13 at 09:42
  • @SriramSakthivel: maybe there's not a great difference here. But it's better anyway since the topic of this question is filtering before processing. Consider that `Path.GetExtension` would be a time-consuming method, you could "stream" the files without waiting for all in memory. Similar to a `StreamReader` when reading lines in a file(note that there's is also a `File.ReadLines` which has the same difference to `File.ReadAllLines`) or a `SqlDataReader` which allows to read one db-record after each other as opposed to a `DataTable`. – Tim Schmelter Oct 11 '13 at 09:47
  • @SriramSakthivel: Another advantage: if you remove the `ToArray` you could even change the query to `Take(100)` for instance. Then you would only enumerate part of all files. Just an example. – Tim Schmelter Oct 11 '13 at 09:52
  • I know that tim, I asked this only because we loop all the files. I already told *if we use `Take(4)` that is different* :) – Sriram Sakthivel Oct 11 '13 at 09:55
  • 1
    @SriramSakthivel: i've overlooked the `Take(4)` completely. However, maybe it's clearer(for people without knowledge of Linq's deferred execution) now. – Tim Schmelter Oct 11 '13 at 09:58
2

use linq

var files = Directory.GetFiles(dir)
    .Where(file=> !file.EndsWith(".txt").ToList();