57

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

110

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

    var files = Directory.GetFiles(jobDir).Where(name => !name.EndsWith(".xml"));
Sign up to request clarification or add additional context in comments.

6 Comments

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.
var files = Directory.GetFiles(jobDir).Where(name => !name.EndsWith(".xml", true));
var files = Directory.GetFiles(jobDir).Where(name => !name.EndsWith(".xml", StringComparison.OrdinalIgnoreCase));
Worked for me. I just had to add .ToList(); at the end because my variable was a list :)
@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!
|
23

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 Comment

I was looking a LINQ way to exclude multiple extensions. I found it here. Thank you very much.
14

You could try something like this:

var allFiles = Directory.GetFiles(@"C:\Path\", "");
var filesToExclude = Directory.GetFiles(@"C:\Path\", "*.txt");
var wantedFiles = allFiles.Except(filesToExclude);

4 Comments

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.
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.
This solution is good for multi pattern file exclusion. Thanks
This will require folder enumeration twice with 2 sets of filters.. Better to play on string arrays only
11

I guess you can use lambda expression

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

Comments

3

You can try this,

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

Comments

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 Comments

Thanks, but manually doing it is something I really don't like
Maybe you can derive your own directory class from the base System.IO.Directory if it is possible (I haven't tried).
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>();

Comments

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"));

Comments

-2

i used that

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

and the PATH is:

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

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.