相匹配的模式在文件夹列表中的文件文件夹、相匹配、模式、文件

由网友(不负如来不负卿)分享简介:我需要列出其中一些模式进行匹配的目录中的文件。我试着用 Directory.GetFiles 播放,但不完全知道为什么它的行为在某些方面。 I need to list files in directory which match some pattern.I tried playing with Direct...

我需要列出其中一些模式进行匹配的目录中的文件。 我试着用 Directory.GetFiles 播放,但不完全 知道为什么它的行为在某些方面。

I need to list files in directory which match some pattern. I tried playing with Directory.GetFiles, but don't fully get why it behaves in some way.

1)例如,该code:

1) For example, this code:

            string[] dirs = Directory.GetFiles(@"c:test", "*t");

            foreach (string dir in dirs)
            {
                Debugger.Log(0, "", dir);
                Debugger.Log(0, "", "n");

            }

输出这样的:

outputs this:

c:test11.11.2007.txtGif
c:test12.1.1990.txt
c:test2.tGift
c:test2.txtGif
c:testtest.txt
...others hidden

您可以看到一些文件结尾 F 而是通过查询仍返回,为什么

You can see some files end with f but were still returned by query, why?

2)此外,这样的:

                string[] dirs = Directory.GetFiles(@"c:test", "*.*.*.txt");


                foreach (string dir in dirs)
                {
                    Debugger.Log(0, "", dir);
                    Debugger.Log(0, "", "n");

                }

返回这样的:

returns this:

c:test1.1.1990.txt
c:test1.31.1990.txt
c:test12.1.1990.txt
c:test12.31.1990.txt

但根据文档( http://msdn.microsoft.com/en-us/library/07wt70x2(V = vs.110)的.aspx )我想这也不得不返回 该文件是在目录:

But according to the documentation (http://msdn.microsoft.com/en-us/library/07wt70x2(v=vs.110).aspx) I think it had to return also this file which is in the directory:

11.11.2007.txtGif

自分机(查询字符串)是3个字母, 但它没有。为什么? (查询时延长为3个字母,医生说它将返回扩展,并与特定的扩展名开始也是如此,例如,见备注)。

since extension (in the query string) is 3 letters long, but it didn't. why? (when query extension is 3 letters long, doc says it will return extensions which start with specified extensions too, e.g., see Remarks).

我是唯一一个谁发现这些结果很奇怪吗?

Am I the only one who finds these results strange?

你有什么建议用当一个人要列出哪些符合特定的模式在文件夹中文件的任何其他的方法呢?

Is there any other approach you would recommend for using when one wants to list files in folder which match certain pattern?

用户在我的情况下,可以任意输入一些图案,我不想靠 方法,我不能确定的结果(如它的发生与GetFiles的)。

User in my case may arbitrarily type some pattern, and I don't want to rely on method which I am unsure about the result (like it happened with GetFiles).

推荐答案

所有这些行为是完全一样的文档中描述你联系。下面是相关位的摘录:

All of this behavior is exactly as described in the documentation you've linked. Here's an excerpt of the pertinent bits:

当您使用星号通配符的是searchPattern这样的   为* .TXT,人物在指定分机数量   影响搜索如下:

When you use the asterisk wildcard character in a searchPattern such as "*.txt", the number of characters in the specified extension affects the search as follows:   

如果指定的扩展名是恰好有三个字符,该方法将返回文件开头指定的扩展   扩展。例如,* .xls的同时返回book.xls和   book.xlsx。 EXCEL中有一些文件的名字列表,一个文件夹中有包含列表中的文件,怎样从文件夹中快速选出列表中的文件

If the specified extension is exactly three characters long, the method returns files with extensions that begin with the specified extension. For example, "*.xls" returns both "book.xls" and "book.xlsx".

在其他情况下,该方法返回文件指定的扩展完全匹配。例如,*。爱返回file.ai,但不是   file.aif。

In all other cases, the method returns files that exactly match the specified extension. For example, "*.ai" returns "file.ai" but not "file.aif".

当您使用问号通配符,此方法返回   只有符合指定的文件扩展名的文件。例如,给定   两个文件,​​file1.txt的和file1.txtother,在一个目录中,搜索   图案文件?.TXT返回刚才的第一个文件,而搜索   * .txt的文件的方式返回这两个文件。 NoteNote

When you use the question mark wildcard character, this method returns only files that match the specified file extension. For example, given two files, "file1.txt" and "file1.txtother", in a directory, a search pattern of "file?.txt" returns just the first file, whereas a search pattern of "file*.txt" returns both files. NoteNote

由于此方法检查对与两个8.3文件的文件名   名格式和长文件名格式,类似一个搜索模式    1 的.TXT可能会返回意外的文件名。例如,使用一   搜索模式的 1 的.TXT收益longfilename.txt,因为   相当于8.3文件名格式为LONGFI〜1.TXT。

Because this method checks against file names with both the 8.3 file name format and the long file name format, a search pattern similar to "1.txt" may return unexpected file names. For example, using a search pattern of "1.txt" returns "longfilename.txt" because the equivalent 8.3 file name format is "LONGFI~1.TXT".

http://msdn.microsoft。 COM / EN-US /库/ wz42302f%28V = vs.110%29.aspx

上面的最后一段寻找时,清楚地说明你的结果* T 。您可以通过使用命令看到这个目录C:测试/ X 显示8.3文件名。在这里, C:测试 11.11.2007.txtGif 匹配 * T ,因为它的8.3文件名为 111120〜1.TXT

The last paragraph above clearly explains your results when searching for *t. You can see this by using the command dir C:test /x to show the 8.3 filenames. Here, C:test11.11.2007.txtGif matches *t because its 8.3 filename is 111120~1.TXT.

有关治疗 *。*。*。TXT ,我认为你要么误跨preting约三个字母的文件扩展名的第一位或许它不是写很清楚。需要注意的是,他们很特别提到的通配符用法的是searchPattern如* .TXT。您的搜索模式不匹配,所以你必须在字里行间有点明白为什么他们对三个字母的文件扩展名的评论也适用于他们给的例子,但不是你的。说真的,我觉得整个顶部可以忽略不计,如果你只是把想法加入的最后一位约8.3文件名。三个字母的文件扩展名通配符后的治疗是真正的8.3文件名搜索行为只是一个副作用。

For the treatment of *.*.*.txt, I think you're either mis-interpreting the first bit about three-letter file extensions or perhaps it wasn't written quite clearly. Note that they quite specifically mentioned wildcard usage 'in a searchPattern such as "*.txt"'. Your search pattern doesn't match that, so you have to read between the lines a bit to see why their comment about three-letter file extensions applies to the example they gave but not yours. Really, I think that whole top section can be ignored if you just put a bit of thought into the last bit about 8.3 filenames. The treatment of three-letter file extensions after wildcards is really just a side-effect of the 8.3 filename search behavior.

考虑他们给了例子:

*。XLS同时返回book.xls和book.xlsx

"*.xls" returns both "book.xls" and "book.xlsx"

这是因为文件名book.xls(包括8.3和长文件名,因为这个名字自然符合8.3)的和的8.3文件名book.xlsx(书〜 1.XLS)相匹配的查询的* .xls。

This is because the filename for "book.xls" (both 8.3 and long filename, since the name naturally complies with 8.3) and the 8.3 filename for "book.xlsx" ("BOOK~1.XLS") matches a query of "*.xls".

*。爱返回file.ai,而不是file.aif

"*.ai" returns "file.ai" but not "file.aif"

这是因为file.ai自然匹配*。爱,而file.aif没有。 8.3搜索行为不来这里发挥作用可言,因为这两个文件名都已经符合8.3。然而,即使他们没有,同样的情况也仍然适用,因为任何8.3文件名以艾扩展名的文件仍然会结束只是艾。

This is because "file.ai" naturally matches "*.ai" while "file.aif" doesn't. 8.3 search behavior doesn't come into play here at all, because both filenames are already 8.3-compliant. However, even if they weren't, the same would still hold true because any 8.3 filename for a file with an extension of ".ai" is still going to end in just ".AI".

的唯一原因,它的问题在搜索文件扩展名是否正是三个字符是因为8.3文件名包含在搜索中,和对象的8.3 FILNAME扩展使用长文件名总是会刚刚在长文件名的最后一个点之后的前三个字符。关键的部分从上面的文档中缺少的是,前三个字符匹配​​完成的只有的对8.3文件名。

The only reason it matters whether or not the file extension in your search is exactly three characters is because the 8.3 filenames are included in the search, and 8.3 filname extensions for objects with long filenames will always have just the first three characters after the last dot in the long filename. The key part missing from the documentation above is that the "first three characters" matching is done only against the 8.3 filename.

那么,让我们来看看你问这里的异常。 (如果你想要的任何其他奇怪的行为解释,超出了你的结果 *:T *。*。*。TXT ,请把它们作为单独的问题。)

So, let's look at the anomalies you're asking about here. (If you want any other strange behaviors explained, beyond your results for *.t and *.*.*.txt, please post them as separate questions.)

用于输出的搜索 * T 包括 11.11.2007.txtGif 2.txtGif

Output of a search for *t includes 11.11.2007.txtGif and 2.txtGif.

这是因为8.3文件名匹配的模式 * T

This is because the 8.3 filenames match a pattern of *t.

11.11.2007.txtGif = 111120〜1.TXT 2.txtGIF = 2BEFD〜1.TXT

(两个8.3文件名结尾T。)

(Both 8.3 filenames end in "T".)

用于输出的搜索 *。*。*。TXT 不包括 11.11.2007.txtGif

Output of a search for *.*.*.txt does not include 11.11.2007.txtGif.

这是因为,无论是长文件名,也不是8.3名,符合的模式*。*。*。TXT

This is because neither the long filename, nor the 8.3 filename, match a pattern of *.*.*.txt.

11.11.2007.txtGif = 111120〜1.TXT

(长文件名不匹配,因为它不以.txt结束,8.3文​​件名不匹配,因为它只有一个点。)

(The long filename doesn't match because it doesn't end in ".txt", and the 8.3 filename doesn't match because it only has one dot.)

阅读全文

相关推荐

最新文章