如何在目录中查找具有特定名称的文件 [英] How to find files in a directory with specific name

查看:93
本文介绍了如何在目录中查找具有特定名称的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须在c#中的名称中包含"ARCHIVE"的目录中找到所有文件.
以下是目录中的示例文件名

Log_726
Log_726_ARCHIVE_20110726080257
Log_726_ARCHIVE_20110726085221

搜索功能应仅返回最后两个文件.

我已经检查了Directory.GetFiles方法,但是当我们需要查找名称以特定字符开头或以特定字符结尾的文件或具有特定扩展名的文件时,似乎可以使用.

我找不到如何搜索文件的名称,文件中包含一些特定的文本.

有人可以帮我吗?

在此先谢谢您.

I have to find all files in a directory which contains "ARCHIVE" in their name in c#.
Below are the sample file name in the directory

Log_726
Log_726_ARCHIVE_20110726080257
Log_726_ARCHIVE_20110726085221

Search function should return only last two files.

I have checked the Directory.GetFiles method, but it seems this works when we need to find a file whose name starts with a particular character or ends with a particular character or files with specific extension.

I could not find how to search files, which contains some particular text in their name.

Can anyone please help me in this?

Thanks in advance.

推荐答案

您说的关于开始,结束字符或扩展名"的说法不正确.文件名中没有扩展名"(文件名),文件名可以是文件系统名允许使用的任何字符序列. (扩展名.''"后跟其他几个字符对于使用已注册的文件类型"的命令行管理程序是必不可少的,但这与文件系统无关.)通配符"*"表示任何字符序列,而通配符?"仅表示一个字符.因此,* _ ARCHIVE_ *应该可以使用.

但是,这里描述了一个细微的问题: Directory.Get.Files搜索模式问题 [ ^ ],并具有解决方法.

—SA
What you say about starting, ending character or "extension" is not true. There are no "extensions" in the file name (anymore), the file name can be any sequence of characters allowed for file system names. (Extensions as ''.'' followed be few other characters are essential for the Shell using registered "file types", but it has nothing to do with file system.) The wildcard ''*'' means any sequence of character and the wildcard ''?'' means exactly one character. So, *_ARCHIVE_* should work.

However, there is one subtle problem described here: Directory.Get.Files search pattern problem[^], with work-around.

—SA


不,您可以使用GetFiles
No, you can use GetFiles
string[] dirs = Directory.GetFiles(@"c:\", "*ARCHIVE*");


参考: http://msdn.microsoft.com/en-us/library/wz42302f.aspx [< ^ ]并检查以下内容的其他变体相同的功能.


Ref:http://msdn.microsoft.com/en-us/library/wz42302f.aspx[^] and check other variants of the same function.


这篇关于如何在目录中查找具有特定名称的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆