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

发布于 2024-07-16 13:49:11 字数 161 浏览 1 评论 0原文

从目录获取文件时如何排除某些文件类型?

我试过了

var files = Directory.GetFiles(jobDir);

,但好像这个功能只能选择你想要包含的文件类型,不能排除。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

尘世孤行 2024-07-23 13:49:12

我知道,这是一个老要求,但对我来说它总是很重要。

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

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)));
}
我三岁 2024-07-23 13:49:12

你可以尝试这样的事情:

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

You could try something like this:

var allFiles = Directory.GetFiles(@"C:\Path\", "");
var filesToExclude = Directory.GetFiles(@"C:\Path\", "*.txt");
var wantedFiles = allFiles.Except(filesToExclude);
聽兲甴掵 2024-07-23 13:49:12

我想你可以使用 lambda 表达式

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

I guess you can use lambda expression

var files = Array.FindAll(Directory.GetFiles(jobDir), x => !x.EndWith(".myext"))
作业与我同在 2024-07-23 13:49:12

你可以试试这个,

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

You can try this,

var directoryInfo = new DirectoryInfo("C:\YourPath");
var filesInfo = directoryInfo.GetFiles().Where(x => x.Extension != ".pdb");
心的位置 2024-07-23 13:49:12

据我所知,没有办法指定排除模式。
您必须手动执行此操作,例如:

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

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);
}
夏末染殇 2024-07-23 13:49:12

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

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

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>();
英雄似剑 2024-07-23 13:49:12

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

例如,搜索模式通配符说明符匹配:

  • *(星号)该位置有零个或多个字符。
  • ? (问号)该位置有零个或一个字符。

这可以用于上述用途,如下所示。

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

或者排除本来会包含的项目。

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

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"));
祁梦 2024-07-23 13:49:12

我用过那个

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

且 PATH 为:

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

i used that

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

and the PATH is:

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

執念 2024-07-23 13:49:11

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

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

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

    var files = Directory.GetFiles(jobDir).Where(name => !name.EndsWith(".xml"));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文