Skip to content

Commit f9f1a1d

Browse files
add missing examples for EnumerateFiles
1 parent 9654822 commit f9f1a1d

File tree

9 files changed

+229
-2
lines changed

9 files changed

+229
-2
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// <Snippet1>
2+
using System;
3+
using System.IO;
4+
using System.Linq;
5+
6+
class Program
7+
{
8+
static void Main(string[] args)
9+
{
10+
try
11+
{
12+
// Set a variable to the My Documents path.
13+
string docPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
14+
15+
// Set the options for the enumeration.
16+
var options = new EnumerationOptions()
17+
{
18+
IgnoreInaccessible = true,
19+
MatchCasing = MatchCasing.CaseInsensitive,
20+
MatchType = MatchType.Simple,
21+
RecurseSubdirectories = true
22+
};
23+
24+
var files = from file in Directory.EnumerateFiles(docPath, "*.txt", options)
25+
from line in File.ReadLines(file)
26+
where line.Contains("Microsoft")
27+
select new
28+
{
29+
File = file,
30+
Line = line
31+
};
32+
33+
foreach (var f in files)
34+
{
35+
Console.WriteLine($"{f.File}\t{f.Line}");
36+
}
37+
38+
Console.WriteLine($"{files.Count()} files found.");
39+
}
40+
catch (PathTooLongException pathEx)
41+
{
42+
Console.WriteLine(pathEx.Message);
43+
}
44+
catch (Exception ex)
45+
{
46+
Console.WriteLine(ex.Message);
47+
}
48+
}
49+
}
50+
// </Snippet1>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// <Snippet16>
2+
using System;
3+
using System.IO;
4+
5+
namespace ConsoleApplication
6+
{
7+
class Program
8+
{
9+
static void Main(string[] args)
10+
{
11+
string sourceDirectory = @"C:\current";
12+
string archiveDirectory = @"C:\archive";
13+
14+
var options = new EnumerationOptions()
15+
{
16+
MatchCasing = MatchCasing.CaseInsensitive,
17+
MatchType = MatchType.Simple,
18+
RecurseSubdirectories = true
19+
};
20+
21+
try
22+
{
23+
var txtFiles = Directory.EnumerateFiles(sourceDirectory, "*.txt", options);
24+
25+
foreach (string currentFile in txtFiles)
26+
{
27+
string fileName = currentFile.Substring(sourceDirectory.Length + 1);
28+
Directory.Move(currentFile, Path.Combine(archiveDirectory, fileName));
29+
}
30+
}
31+
catch (Exception ex)
32+
{
33+
Console.WriteLine(ex.Message);
34+
}
35+
}
36+
}
37+
}
38+
// </Snippet16>

snippets/fsharp/System.IO/Directory/EnumerateFiles/fs.fsproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@
88
<Compile Include="program1.fs" />
99
<Compile Include="program2.fs" />
1010
<Compile Include="program.fs" />
11+
<Compile Include="program3.fs" />
1112
</ItemGroup>
12-
</Project>
13+
</Project>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
module program3
2+
3+
// <Snippet1>
4+
open System
5+
open System.IO
6+
7+
try
8+
// Set a variable to the My Documents path.
9+
let docPath =
10+
Environment.GetFolderPath Environment.SpecialFolder.MyDocuments
11+
12+
// Set the options for the enumeration.
13+
let options = new EnumerationOptions(
14+
IgnoreInaccessible = true,
15+
MatchCasing = MatchCasing.CaseInsensitive,
16+
MatchType = MatchType.Simple,
17+
RecurseSubdirectories = true
18+
)
19+
20+
let files =
21+
query {
22+
for file in Directory.EnumerateFiles(docPath, "*.txt", options) do
23+
for line in File.ReadLines file do
24+
where (line.Contains "Microsoft")
25+
select {| File = file; Line = line |}
26+
}
27+
28+
for f in files do
29+
printfn $"{f.File}\t{f.Line}"
30+
31+
printfn $"{Seq.length files} files found."
32+
with
33+
| :? PathTooLongException as pathEx -> printfn $"{pathEx.Message}"
34+
| ex -> printfn $"{ex.Message}"
35+
// </Snippet1>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
module class8
2+
3+
// <Snippet16>
4+
open System.IO
5+
6+
let sourceDirectory = @"C:\current"
7+
let archiveDirectory = @"C:\archive"
8+
9+
let options = new EnumerationOptions(
10+
MatchCasing = MatchCasing.CaseInsensitive,
11+
MatchType = MatchType.Simple,
12+
RecurseSubdirectories = true
13+
)
14+
15+
try
16+
let txtFiles = Directory.EnumerateFiles(sourceDirectory, "*.txt", options)
17+
18+
for currentFile in txtFiles do
19+
let fileName = currentFile.Substring(sourceDirectory.Length + 1)
20+
Directory.Move(currentFile, Path.Combine(archiveDirectory, fileName))
21+
with ex ->
22+
printfn $"{ex.Message}"
23+
// </Snippet16>

snippets/fsharp/System.IO/Directory/Overview/fs.fsproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@
1212
<Compile Include="class4.fs" />
1313
<Compile Include="class5.fs" />
1414
<Compile Include="class1.fs" />
15+
<Compile Include="class8.fs" />
1516
</ItemGroup>
16-
</Project>
17+
</Project>
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
' <Snippet1>
2+
Imports System.IO
3+
Imports System.Xml.Linq
4+
5+
Module Module1
6+
Sub Main()
7+
Try
8+
' Set a variable to the My Documents Path.
9+
Dim docPath As String = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
10+
11+
' Set the options for the enumeration.
12+
Dim options As New EnumerationOptions() With {
13+
.IgnoreInaccessible = True,
14+
.MatchCasing = MatchCasing.CaseInsensitive,
15+
.MatchType = MatchType.Simple,
16+
.RecurseSubdirectories = True
17+
}
18+
19+
Dim files = From chkFile In Directory.EnumerateFiles(docPath, "*.txt", options)
20+
From line In File.ReadLines(chkFile)
21+
Where line.Contains("Microsoft")
22+
Select New With {
23+
.curFile = chkFile,
24+
.curLine = line
25+
}
26+
27+
For Each f In files
28+
Console.WriteLine($"{f.curFile}\t{f.curLine}")
29+
Next
30+
31+
Console.WriteLine($"{files.Count} files found.")
32+
Catch pathEx As PathTooLongException
33+
Console.WriteLine(pathEx.Message)
34+
Catch ex As Exception
35+
Console.WriteLine(ex.Message)
36+
End Try
37+
End Sub
38+
End Module
39+
' </Snippet1>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
' <Snippet16>
2+
Imports System.IO
3+
4+
Module Module1
5+
Sub Main()
6+
Dim sourceDirectory As String = "C:\current"
7+
Dim archiveDirectory As String = "C:\archive"
8+
9+
Dim options As New EnumerationOptions() With {
10+
.MatchCasing = MatchCasing.CaseInsensitive,
11+
.MatchType = MatchType.Simple,
12+
.RecurseSubdirectories = True
13+
}
14+
15+
Try
16+
Dim txtFiles = Directory.EnumerateFiles(sourceDirectory, "*.txt", options)
17+
18+
For Each currentFile As String In txtFiles
19+
Dim fileName = currentFile.Substring(sourceDirectory.Length + 1)
20+
Directory.Move(currentFile, Path.Combine(archiveDirectory, fileName))
21+
Next
22+
Catch ex As Exception
23+
Console.WriteLine(ex.Message)
24+
End Try
25+
End Sub
26+
End Module
27+
' </Snippet16>

xml/System.IO/Directory.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1377,6 +1377,19 @@ The returned collection is not cached. Each call to the <xref:System.Collections
13771377
13781378
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.
13791379
1380+
## Examples
1381+
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.
1382+
1383+
:::code language="csharp" source="~/snippets/csharp/System.IO/Directory/Overview/class8.cs" id="Snippet16":::
1384+
:::code language="fsharp" source="~/snippets/fsharp/System.IO/Directory/Overview/class8.fs" id="Snippet16":::
1385+
:::code language="vb" source="~/snippets/visualbasic/System.IO/Directory/Overview/class8.vb" id="Snippet16":::
1386+
1387+
The following example recursively enumerates all files with the `.txt` extension, ignoring inaccessible directories and files. It reads each line of the file and displays the line if it contains the string "Microsoft".
1388+
1389+
:::code language="csharp" source="~/snippets/csharp/System.IO/Directory/EnumerateFiles/program3.cs" id="Snippet1":::
1390+
:::code language="fsharp" source="~/snippets/fsharp/System.IO/Directory/EnumerateFiles/program3.fs" id="Snippet1":::
1391+
:::code language="vb" source="~/snippets/visualbasic/System.IO/Directory/EnumerateFiles/program2.vb" id="Snippet1":::
1392+
13801393
]]></format>
13811394
</remarks>
13821395
<exception cref="T:System.ArgumentException">.NET Framework and .NET Core versions older than 2.1: <paramref name="path" /> is a zero-length string, contains only white space, or contains invalid characters. You can query for invalid characters by using the <see cref="M:System.IO.Path.GetInvalidPathChars" /> method.

0 commit comments

Comments
 (0)