Skip to content

Instantly share code, notes, and snippets.

@AlexArchive
Created September 8, 2014 13:49
Show Gist options
  • Save AlexArchive/dd3b8402866cf890b07b to your computer and use it in GitHub Desktop.
Save AlexArchive/dd3b8402866cf890b07b to your computer and use it in GitHub Desktop.
(F# versus. C#) Print all file paths in both top level directories and sub directories.
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace ConsoleApplication9
{
class Program
{
static void Main()
{
using (var writer = new StreamWriter("C:\\Users\\ByteBlast\\Desktop\\dump.txt"))
{
foreach (var file in AllFilesUnder("M:/othr"))
writer.WriteLine(file);
}
}
static IEnumerable<string> AllFilesUnder(string basePath)
{
foreach (var file in Directory.GetFiles(basePath))
yield return file;
foreach (var x in Directory.GetDirectories(basePath).Select(AllFilesUnder).SelectMany(files => files))
yield return x;
}
}
}
open System.IO;
let rec allFilesUnder basePath =
seq {
yield! Directory.GetFiles(basePath)
for subdir in Directory.GetDirectories(basePath) do
yield! allFilesUnder subdir
}
[<EntryPoint>]
let main args =
use writer = new StreamWriter("C:\\Users\\ByteBlast\\Desktop\\dump.txt")
(allFilesUnder "M:/othr") |> Seq.iter writer.WriteLine
0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment