Fsharp: List Dir Content (walk dir)

By Xah Lee. Date: . Last updated: .

get a list

// 2025-12-29
// list dir content

open System.IO

let xpath = @"c:/Users/xah/web/xahlee_info/fsharp/"

let fileList = Directory.GetFiles(xpath)
// Returns string[] of full paths

printfn "%A" fileList

(*
[|"c:/Users/xah/web/xahlee_info/fsharp\fsharp_array.html";
  "c:/Users/xah/web/xahlee_info/fsharp\fsharp_at_sign_string.html";
  ...
  "c:/Users/xah/web/xahlee_info/fsharp\fsharp_write_file.html"|]
*)

get a seq

open System.IO

let xpath = @"c:/Users/xah/web/xahlee_info/fsharp/"

// walk dir. list all .html files.
let filesSeq = Directory.EnumerateFiles(xpath, "*.html")
// Returns seq<string>

filesSeq |> Seq.iter (printfn "%s")

(*
c:/Users/xah/web/xahlee_info/fsharp/fsharp_array.html
c:/Users/xah/web/xahlee_info/fsharp/fsharp_at_sign_string.html
c:/Users/xah/web/xahlee_info/fsharp/fsharp_boolean.html
...
*)

list dir and all subdirs

open System.IO

let xpath = "c:/Users/xah/web/xahlee_info/fsharp/"

// All files and nested dirs

let xfileseq = Directory.EnumerateFiles(xpath, "*.html", SearchOption.AllDirectories)

xfileseq |> Seq.iter (printfn "%s")

(*
c:/Users/xah/web/xahlee_info/fsharp/fsharp_array.html
c:/Users/xah/web/xahlee_info/fsharp/fsharp_at_sign_string.html
...
 *)

// Or with DirectoryInfo
// dirInfo.EnumerateFiles("*.*", SearchOption.AllDirectories)
// |> Seq.iter (fun fi -> printfn "%s" fi.FullName)

fsharp, file operations