Skip to content

Commit 7c48fbb

Browse files
authored
Fix note about ? wildcard in EnumerateFiles() (dotnet#7612)
1 parent 829aef2 commit 7c48fbb

File tree

2 files changed

+22
-45
lines changed

2 files changed

+22
-45
lines changed
Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
> [!NOTE]
2-
> **.NET Framework only:** When you use the asterisk wildcard character in `searchPattern` and you specify a three-character file extension, for example, "\*.txt", this method also returns files with extensions that *begin* with the specified extension. For example, the search pattern "\*.xls" returns both "book.xls" and "book.xlsx". This behavior only occurs if an asterisk is used in the search pattern and the file extension provided is exactly three characters. If you use the question mark wildcard character instead of the asterisk, this method returns only files that match the specified file extension exactly. The following table depicts this anomaly in .NET Framework.
2+
> **.NET Framework only:** When you use the asterisk wildcard character in `searchPattern` and you specify a three-character file extension, for example, "\*.txt", this method also returns files with extensions that *begin* with the specified extension. For example, the search pattern "\*.xls" returns both "book.xls" and "book.xlsx". This behavior only occurs if an asterisk is used in the search pattern and the file extension provided is exactly three characters. If you use the question mark wildcard character somewhere in the search pattern, this method returns only files that match the specified file extension exactly. The following table depicts this anomaly in .NET Framework.
33
>
4-
> | Files in directory | Search pattern | .NET 5+ returns | .NET Framework returns |
5-
> |---------------------|----------------|-------------------|-------------------------|
6-
> | file.ai, file.aif | *.ai | file.ai | file.ai |
7-
> | book.xls, book.xlsx | *.xls | book.xls | **book.xls, book.xlsx** |
8-
> | file.ai, file.aif | ?.ai | file.ai | file.ai |
9-
> | book.xls, book.xlsx | ?.xls | book.xls | book.xls |
4+
> | Files in directory | Search pattern | .NET 5+ returns | .NET Framework returns |
5+
> |---------------------------------|----------------|---------------------|-------------------------|
6+
> | file.ai, file.aif | *.ai | file.ai | file.ai |
7+
> | book.xls, book.xlsx | *.xls | book.xls | **book.xls, book.xlsx** |
8+
> | ello.txt, hello.txt, hello.txtt | ?ello.txt | ello.txt, hello.txt | ello.txt, hello.txt |

xml/System.IO/Directory.xml

Lines changed: 16 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1246,7 +1246,7 @@ An I/O error occurred.</exception>
12461246
12471247
The <xref:System.IO.Directory.EnumerateFiles%2A> and <xref:System.IO.Directory.GetFiles%2A> methods differ as follows: When you use <xref:System.IO.Directory.EnumerateFiles%2A>, you can start enumerating the collection of names before the whole collection is returned; when you use <xref:System.IO.Directory.GetFiles%2A>, you must wait for the whole array of names to be returned before you can access the array. Therefore, when you are working with many files and directories, <xref:System.IO.Directory.EnumerateFiles%2A> can be more efficient.
12481248
1249-
The returned collection is not cached; each call to the <xref:System.Collections.Generic.IEnumerable%601.GetEnumerator%2A> on the collection will start a new enumeration.
1249+
The returned collection is not cached. Each call to the <xref:System.Collections.Generic.IEnumerable%601.GetEnumerator%2A> on the collection starts a new enumeration.
12501250
12511251
## Examples
12521252
The following example shows how to retrieve all the text files in a directory and move them to a new directory. After the files are moved, they no longer exist in the original directory.
@@ -1346,7 +1346,7 @@ An I/O error occurred.</exception>
13461346
13471347
The <xref:System.IO.Directory.EnumerateFiles%2A> and <xref:System.IO.Directory.GetFiles%2A> methods differ as follows: When you use <xref:System.IO.Directory.EnumerateFiles%2A>, you can start enumerating the collection of names before the whole collection is returned. When you use <xref:System.IO.Directory.GetFiles%2A>, you must wait for the whole array of names to be returned before you can access the array. Therefore, when you are working with many files and directories, <xref:System.IO.Directory.EnumerateFiles%2A> can be more efficient.
13481348
1349-
The returned collection is not cached; each call to the <xref:System.Collections.Generic.IEnumerable%601.GetEnumerator%2A> on the collection will start a new enumeration.
1349+
The returned collection is not cached. Each call to the <xref:System.Collections.Generic.IEnumerable%601.GetEnumerator%2A> on the collection starts a new enumeration.
13501350
13511351
]]></format>
13521352
</remarks>
@@ -1440,7 +1440,7 @@ An I/O error occurred.</exception>
14401440
14411441
The <xref:System.IO.Directory.EnumerateFiles%2A> and <xref:System.IO.Directory.GetFiles%2A> methods differ as follows: When you use <xref:System.IO.Directory.EnumerateFiles%2A>, you can start enumerating the collection of names before the whole collection is returned. When you use <xref:System.IO.Directory.GetFiles%2A>, you must wait for the whole array of names to be returned before you can access the array. Therefore, when you are working with many files and directories, <xref:System.IO.Directory.EnumerateFiles%2A> can be more efficient.
14421442
1443-
The returned collection is not cached; each call to the <xref:System.Collections.Generic.IEnumerable%601.GetEnumerator%2A> on the collection will start a new enumeration.
1443+
The returned collection is not cached. Each call to the <xref:System.Collections.Generic.IEnumerable%601.GetEnumerator%2A> on the collection starts a new enumeration.
14441444
14451445
## Examples
14461446
The following example shows how to retrieve all the text files in a directory and its subdirectories, and move them to a new directory. After the files are moved, they no longer exist in the original directories.
@@ -1449,7 +1449,7 @@ An I/O error occurred.</exception>
14491449
:::code language="fsharp" source="~/snippets/fsharp/System.IO/Directory/Overview/class5.fs" id="Snippet13":::
14501450
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.IO.Directory/VB/class5.vb" id="Snippet13":::
14511451
1452-
The following example recursively enumerates all files that have a .txt extension, reads each line of the file, and displays the line if it contains the string "Microsoft".
1452+
The following example recursively enumerates all files that have the extension `.txt`, reads each line of the file, and displays the line if it contains the string "Microsoft".
14531453
14541454
:::code language="csharp" source="~/snippets/csharp/System.IO/Directory/EnumerateFiles/program.cs" id="Snippet1":::
14551455
:::code language="fsharp" source="~/snippets/fsharp/System.IO/Directory/EnumerateFiles/program.fs" id="Snippet1":::
@@ -2896,9 +2896,7 @@ An I/O error occurred.</exception>
28962896
28972897
The case-sensitivity of the `path` parameter corresponds to that of the file system on which the code is running. For example, it's case-insensitive on NTFS (the default Windows file system) and case-sensitive on Linux file systems.
28982898
2899-
For a list of common I/O tasks, see [Common I/O Tasks](/dotnet/standard/io/common-i-o-tasks).
2900-
2901-
2899+
For a list of common I/O tasks, see [Common I/O Tasks](/dotnet/standard/io/common-i-o-tasks).
29022900
29032901
## Examples
29042902
The following example demonstrates how to use the <xref:System.IO.Directory.GetFiles%2A> method to return file names from a user-specified location. The example is configured to catch all errors common to this method.
@@ -2991,27 +2989,19 @@ An I/O error occurred.</exception>
29912989
Characters other than the wildcard are literal characters. For example, the `searchPattern` string "\*t" searches for all names in `path` ending with the letter "t". The `searchPattern` string "s\*" searches for all names in `path` beginning with the letter "s".
29922990
29932991
`searchPattern` cannot end in two periods ("..") or contain two periods ("..") followed by <xref:System.IO.Path.DirectorySeparatorChar> or <xref:System.IO.Path.AltDirectorySeparatorChar>, nor can it contain any invalid characters. You can query for invalid characters by using the <xref:System.IO.Path.GetInvalidPathChars%2A> method.
2992+
2993+
[!INCLUDE[enumerate-files-net-framework](~/includes/enumerate-files-net-framework.md)]
29942994
29952995
> [!NOTE]
2996-
> 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:
2997-
>
2998-
> - 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".
2999-
> - In all other cases, the method returns files that exactly match the specified extension. For example, "\*.ai" returns "file.ai" but not "file.aif".
3000-
>
3001-
> 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.
3002-
3003-
> [!NOTE]
3004-
> 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".
2996+
> 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".
30052997
30062998
The <xref:System.IO.Directory.EnumerateFiles%2A> and <xref:System.IO.Directory.GetFiles%2A> methods differ as follows: When you use <xref:System.IO.Directory.EnumerateFiles%2A>, you can start enumerating the collection of names before the whole collection is returned; when you use <xref:System.IO.Directory.GetFiles%2A>, you must wait for the whole array of names to be returned before you can access the array. Therefore, when you are working with many files and directories, <xref:System.IO.Directory.EnumerateFiles%2A> can be more efficient.
30072999
30083000
The `path` parameter can specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. To obtain the current working directory, see <xref:System.IO.Directory.GetCurrentDirectory%2A>.
30093001
30103002
The case-sensitivity of the `path` parameter corresponds to that of the file system on which the code is running. For example, it's case-insensitive on NTFS (the default Windows file system) and case-sensitive on Linux file systems.
30113003
3012-
For a list of common I/O tasks, see [Common I/O Tasks](/dotnet/standard/io/common-i-o-tasks).
3013-
3014-
3004+
For a list of common I/O tasks, see [Common I/O Tasks](/dotnet/standard/io/common-i-o-tasks).
30153005
30163006
## Examples
30173007
The following example counts the number of files that begin with the specified letter.
@@ -3103,16 +3093,10 @@ An I/O error occurred.</exception>
31033093
31043094
`searchPattern` cannot end in two periods ("..") or contain two periods ("..") followed by <xref:System.IO.Path.DirectorySeparatorChar> or <xref:System.IO.Path.AltDirectorySeparatorChar>, nor can it contain any invalid characters. You can query for invalid characters by using the <xref:System.IO.Path.GetInvalidPathChars%2A> method.
31053095
3106-
> [!NOTE]
3107-
> 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:
3108-
>
3109-
> - 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".
3110-
> - In all other cases, the method returns files that exactly match the specified extension. For example, "\*.ai" returns "file.ai" but not "file.aif".
3111-
>
3112-
> 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.
3096+
[!INCLUDE[enumerate-files-net-framework](~/includes/enumerate-files-net-framework.md)]
31133097
31143098
> [!NOTE]
3115-
> 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".
3099+
> 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".
31163100
31173101
The <xref:System.IO.Directory.EnumerateFiles%2A> and <xref:System.IO.Directory.GetFiles%2A> methods differ as follows: When you use <xref:System.IO.Directory.EnumerateFiles%2A>, you can start enumerating the collection of names before the whole collection is returned; when you use <xref:System.IO.Directory.GetFiles%2A>, you must wait for the whole array of names to be returned before you can access the array. Therefore, when you are working with many files and directories, <xref:System.IO.Directory.EnumerateFiles%2A> can be more efficient.
31183102
@@ -3206,18 +3190,12 @@ An I/O error occurred.</exception>
32063190
32073191
Characters other than the wildcard are literal characters. For example, the `searchPattern` string "\*t" searches for all names in `path` ending with the letter "t". The `searchPattern` string "s\*" searches for all names in `path` beginning with the letter "s".
32083192
3209-
`searchPattern` cannot end in two periods ("..") or contain two periods ("..") followed by <xref:System.IO.Path.DirectorySeparatorChar> or <xref:System.IO.Path.AltDirectorySeparatorChar>, nor can it contain any invalid characters. You can query for invalid characters by using the <xref:System.IO.Path.GetInvalidPathChars%2A> method.
3210-
3211-
> [!NOTE]
3212-
> 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:
3213-
>
3214-
> - 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".
3215-
> - In all other cases, the method returns files that exactly match the specified extension. For example, "\*.ai" returns "file.ai" but not "file.aif".
3216-
>
3217-
> 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.
3218-
3193+
`searchPattern` cannot end in two periods ("..") or contain two periods ("..") followed by <xref:System.IO.Path.DirectorySeparatorChar> or <xref:System.IO.Path.AltDirectorySeparatorChar>, nor can it contain any invalid characters. You can query for invalid characters by using the <xref:System.IO.Path.GetInvalidPathChars%2A> method.
3194+
3195+
[!INCLUDE[enumerate-files-net-framework](~/includes/enumerate-files-net-framework.md)]
3196+
32193197
> [!NOTE]
3220-
> 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".
3198+
> 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".
32213199
32223200
The <xref:System.IO.Directory.EnumerateFiles%2A> and <xref:System.IO.Directory.GetFiles%2A> methods differ as follows: When you use <xref:System.IO.Directory.EnumerateFiles%2A>, you can start enumerating the collection of names before the whole collection is returned; when you use <xref:System.IO.Directory.GetFiles%2A>, you must wait for the whole array of names to be returned before you can access the array. Therefore, when you are working with many files and directories, <xref:System.IO.Directory.EnumerateFiles%2A> can be more efficient.
32233201

0 commit comments

Comments
 (0)