56

How to exclude certain file type when getting files from a directory?

I tried

var files = Directory.GetFiles(jobDir);

But it seems that this function can only choose the file types you want to include, not exclude.

3

9 Answers 9

109

You should filter these files yourself, you can write something like this:

    var files = Directory.GetFiles(jobDir).Where(name => !name.EndsWith(".xml"));
6
  • As far as i now the EndsWith takes capital letters into account, which means that if the filetype would be .XML it would not exclude it. Sep 27, 2014 at 9:32
  • 4
    var files = Directory.GetFiles(jobDir).Where(name => !name.EndsWith(".xml", true)); Nov 8, 2014 at 22:47
  • 4
    var files = Directory.GetFiles(jobDir).Where(name => !name.EndsWith(".xml", StringComparison.OrdinalIgnoreCase)); Oct 6, 2017 at 14:08
  • 1
    Worked for me. I just had to add .ToList(); at the end because my variable was a list :) Jan 10, 2020 at 15:30
  • @m-fawad-surosh Thanks, good comment, I needed a slightly different variant, since the original GetFiles returns string[] and I wanted to keep my code with the same convention, I just did a call similar to: Directory.GetFiles(jobDir).Where(...).ToArra(), there are more such options!
    – Juv
    Feb 24, 2021 at 11:26
21

I know, this a old request, but about me it's always important.

if you want exlude a list of file extension: (based on https://stackoverflow.com/a/19961761/1970301)

var exts = new[] { ".mp3", ".jpg" };



public IEnumerable<string> FilterFiles(string path, params string[] exts) {
    return
        Directory
        .GetFiles(path)
        .Where(file => !exts.Any(x => file.EndsWith(x, StringComparison.OrdinalIgnoreCase)));
}
1
  • 1
    I was looking a LINQ way to exclude multiple extensions. I found it here. Thank you very much.
    – WpfBee
    May 11, 2018 at 5:01
15

You could try something like this:

var allFiles = Directory.GetFiles(@"C:\Path\", "");
var filesToExclude = Directory.GetFiles(@"C:\Path\", "*.txt");
var wantedFiles = allFiles.Except(filesToExclude);
3
  • This works brilliantly in all forms of the GetFiles() command. It doesn't solve the question precisely but you get the added benefit of leveraging the searchPattern parameter, which is much more powerful than just looking at extensions. Jun 14, 2012 at 18:30
  • 2
    Advice: this won't work with the instance method DirectoryInfo.GetFiles(): it returns an array of FileInfo (instead string), a non-comparable/non-equatable class.
    – T-moty
    Feb 13, 2015 at 14:32
  • 1
    This solution is good for multi pattern file exclusion. Thanks
    – Abhay
    Feb 9, 2019 at 16:03
11

I guess you can use lambda expression

var files = Array.FindAll(Directory.GetFiles(jobDir), x => !x.EndWith(".myext"))
0
3

You can try this,

var directoryInfo = new DirectoryInfo("C:\YourPath");
var filesInfo = directoryInfo.GetFiles().Where(x => x.Extension != ".pdb");
0
0

Afaik there is no way to specify the exclude patterns. You have to do it manually, like:

string[] files = Directory.GetFiles(myDir);
foreach(string fileName in files)
{
    DoSomething(fileName);
}
2
  • 2
    Thanks, but manually doing it is something I really don't like
    – Graviton
    Apr 16, 2009 at 8:10
  • Maybe you can derive your own directory class from the base System.IO.Directory if it is possible (I haven't tried).
    – Biri
    Apr 16, 2009 at 8:16
0

This is my version on the answers I read above

List<FileInfo> fileInfoList = ((DirectoryInfo)new DirectoryInfo(myPath)).GetFiles(fileNameOnly + "*").Where(x => !x.Name.EndsWith(".pdf")).ToList<FileInfo>();
0

I came across this looking for a method to do this where the exclusion could use the search pattern rules and not just EndWith type logic.

e.g. Search pattern wildcard specifier matches:

  • * (asterisk) Zero or more characters in that position.
  • ? (question mark) Zero or one character in that position.

This could be used for the above as follows.

string dir = @"C:\Temp";
var items = Directory.GetFiles(dir, "*.*").Except(Directory.GetFiles(dir, "*.xml"));

Or to exclude items that would otherwise be included.

string dir = @"C:\Temp";
var items = Directory.GetFiles(dir, "*.txt").Except(Directory.GetFiles(dir, "*HOLD*.txt"));
-2

i used that

Directory.GetFiles(PATH, "*.dll"))

and the PATH is:

public static string _PATH = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.