Exclude certain file extensions when getting files from a directory

C#FileDirectory

C# Problem Overview


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.

C# Solutions


Solution 1 - C#

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

    var files = Directory.GetFiles(jobDir).Where(name => !name.EndsWith(".xml"));

Solution 2 - C#

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

Solution 3 - C#

You could try something like this:

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

Solution 4 - C#

I guess you can use lambda expression

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

Solution 5 - C#

You can try this,

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

Solution 6 - C#

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

Solution 7 - C#

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

Solution 8 - C#

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

Solution 9 - C#

i used that

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

and the PATH is:

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGravitonView Question on Stackoverflow
Solution 1 - C#okutaneView Answer on Stackoverflow
Solution 2 - C#pask23View Answer on Stackoverflow
Solution 3 - C#Julien PoulinView Answer on Stackoverflow
Solution 4 - C#oscarkuoView Answer on Stackoverflow
Solution 5 - C#mvkView Answer on Stackoverflow
Solution 6 - C#BiriView Answer on Stackoverflow
Solution 7 - C#user3071434View Answer on Stackoverflow
Solution 8 - C#WoollyView Answer on Stackoverflow
Solution 9 - C#Alberto EstebanView Answer on Stackoverflow