简体   繁体   English

从目录中获取文件时排除某些文件扩展名

[英]Exclude certain file extensions when getting files from a directory

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.但是这个function好像只能选择你要包含的文件类型,不能排除。

你应该自己过滤这些文件,你可以这样写:

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

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 )如果您想排除文件扩展名列表:(基于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)));
}

You could try something like this:你可以尝试这样的事情:

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

我猜你可以使用 lambda 表达式

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

You can try this,你可以试试这个

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

这是我在上面阅读的答案的版本

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

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.我遇到了这个,正在寻找一种方法来执行此操作,其中排除可以使用搜索模式规则而不仅仅是 EndWith 类型逻辑。

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

Afaik there is no way to specify the exclude patterns. Afaik 无法指定排除模式。 You have to do it manually, like:您必须手动执行此操作,例如:

string[] files = Directory.GetFiles(myDir);
foreach(string fileName in files)
{
    DoSomething(fileName);
}

i used that我用过

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

and the PATH is:路径是:

public static string _PATH = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);公共静态字符串_PATH = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

string[] filesToDelete = Directory.GetFiles(@"C:\Temp", "*.der");

foreach (string fileName in filesToDelete)
{
    File.Delete(fileName);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM