Skip to content

Commit abf4ecb

Browse files
authored
System.IO.File F# snippets (#8627)
* File F# snippets * fix build
1 parent eb0ee01 commit abf4ecb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+1032
-0
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net6.0</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<Compile Include="program.fs" />
9+
</ItemGroup>
10+
</Project>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
module program
2+
// <Snippet1>
3+
open System
4+
open System.IO
5+
6+
let dataPath = @"c:\temp\timestamps.txt"
7+
8+
let createSampleFile () =
9+
let timeStamp = DateTime(1700, 1, 1)
10+
11+
use sw = new StreamWriter(dataPath)
12+
13+
for i = 0 to 499 do
14+
let ts1 = timeStamp.AddYears i
15+
let ts2 = ts1.AddMonths i
16+
let ts3 = ts2.AddDays i
17+
ts3.ToLongDateString() |> sw.WriteLine
18+
19+
createSampleFile ()
20+
21+
let julyWeekends =
22+
File.ReadLines dataPath
23+
|> Seq.filter (fun line ->
24+
(line.StartsWith "Saturday"
25+
|| line.StartsWith "Sunday")
26+
&& line.Contains "July")
27+
28+
File.WriteAllLines(@"C:\temp\selectedDays.txt", julyWeekends)
29+
30+
let marchMondays =
31+
File.ReadLines dataPath
32+
|> Seq.filter (fun line -> line.StartsWith "Monday" && line.Contains "March")
33+
34+
File.AppendAllLines(@"C:\temp\selectedDays.txt", marchMondays)
35+
36+
// </Snippet1>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
module AllText
2+
//<snippet00>
3+
open System
4+
open System.IO
5+
6+
let path = @"c:\temp\MyTest.txt"
7+
8+
// This text is added only once to the file.
9+
if File.Exists path |> not then
10+
// Create a file to write to.
11+
let createText =
12+
"Hello and Welcome" + Environment.NewLine
13+
14+
File.WriteAllText(path, createText)
15+
16+
// This text is always added, making the file longer over time
17+
// if it is not deleted.
18+
let appendText =
19+
"This is extra text" + Environment.NewLine
20+
21+
File.AppendAllText(path, appendText)
22+
23+
// Open the file to read from.
24+
let readText = File.ReadAllText path
25+
printfn $"{readText}"
26+
//</snippet00>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
module AllText1
2+
//<snippet00>
3+
open System
4+
open System.IO
5+
open System.Text
6+
7+
let path = @"c:\temp\MyTest.txt"
8+
9+
// This text is added only once to the file.
10+
if File.Exists path |> not then
11+
// Create a file to write to.
12+
let createText =
13+
"Hello and Welcome" + Environment.NewLine
14+
15+
File.WriteAllText(path, createText, Encoding.UTF8)
16+
17+
// This text is always added, making the file longer over time
18+
// if it is not deleted.
19+
let appendText =
20+
"This is extra text" + Environment.NewLine
21+
22+
File.AppendAllText(path, appendText, Encoding.UTF8)
23+
24+
// Open the file to read from.
25+
let readText = File.ReadAllText path
26+
printfn $"{readText}"
27+
//</snippet00>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net6.0</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<Compile Include="AllText.fs" />
9+
<Compile Include="AllText1.fs" />
10+
</ItemGroup>
11+
</Project>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
module file_appendtext
2+
// <Snippet1>
3+
open System.IO
4+
5+
let path = @"c:\temp\MyTest.txt"
6+
7+
// This text is added only once to the file.
8+
if File.Exists path |> not then
9+
// Create a file to write to.
10+
use sw = File.CreateText path
11+
sw.WriteLine "Hello"
12+
sw.WriteLine "And"
13+
sw.WriteLine "Welcome"
14+
15+
// This text is always added, making the file longer over time
16+
// if it is not deleted.
17+
do
18+
use sw = File.AppendText path
19+
sw.WriteLine "This"
20+
sw.WriteLine "is Extra"
21+
sw.WriteLine "Text"
22+
23+
// Open the file to read from.
24+
do
25+
use sr = File.OpenText path
26+
27+
let mutable s = sr.ReadLine()
28+
29+
while isNull s |> not do
30+
printfn $"{s}"
31+
s <- sr.ReadLine()
32+
// </Snippet1>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net6.0</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<Compile Include="file_appendtext.fs" />
9+
</ItemGroup>
10+
</Project>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net6.0</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<Compile Include="program.fs" />
9+
</ItemGroup>
10+
</Project>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
open System.IO
2+
3+
// <Snippet1>
4+
let sourceDir = @"c:\current"
5+
let backupDir = @"c:\archives\2008"
6+
7+
try
8+
let picList = Directory.GetFiles(sourceDir, "*.jpg")
9+
let txtList = Directory.GetFiles(sourceDir, "*.txt")
10+
11+
// Copy picture files.
12+
for f in picList do
13+
// Remove path from the file name.
14+
let fName = f.Substring(sourceDir.Length + 1)
15+
16+
// Use the Path.Combine method to safely append the file name to the path.
17+
// Will overwrite if the destination file already exists.
18+
File.Copy(Path.Combine(sourceDir, fName), Path.Combine(backupDir, fName), true)
19+
20+
// Copy text files.
21+
for f in txtList do
22+
// Remove path from the file name.
23+
let fName = f.Substring(sourceDir.Length + 1)
24+
25+
try
26+
// Will not overwrite if the destination file already exists.
27+
File.Copy(Path.Combine(sourceDir, fName), Path.Combine(backupDir, fName))
28+
29+
// Catch exception if the file was already copied.
30+
with
31+
| :? IOException as copyError -> printfn $"{copyError.Message}"
32+
33+
// Delete source files that were copied.
34+
for f in txtList do
35+
File.Delete f
36+
37+
for f in picList do
38+
File.Delete f
39+
40+
// Catch exception if the file was already copied.
41+
with
42+
| :? DirectoryNotFoundException as dirNotFound -> printfn $"{dirNotFound.Message}"
43+
44+
// </Snippet1>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
module filecreate1
2+
3+
// <Snippet1>
4+
open System.IO
5+
open System.Text
6+
7+
let path = @"c:\temp\MyTest.txt"
8+
9+
// Create the file, or overwrite if the file exists.
10+
do
11+
use fs = File.Create path
12+
13+
let info =
14+
UTF8Encoding(true)
15+
.GetBytes "This is some text in the file."
16+
// Add some information to the file.
17+
fs.Write(info, 0, info.Length)
18+
19+
// Open the stream and read it back.
20+
do
21+
use sr = File.OpenText path
22+
let mutable s = sr.ReadLine()
23+
24+
while isNull s |> not do
25+
printfn $"{s}"
26+
s <- sr.ReadLine()
27+
// </Snippet1>

0 commit comments

Comments
 (0)