Skip to content

Instantly share code, notes, and snippets.

@jwosty
Last active March 23, 2016 17:38
Show Gist options
  • Save jwosty/84e1bcc234e7288f9a5a to your computer and use it in GitHub Desktop.
Save jwosty/84e1bcc234e7288f9a5a to your computer and use it in GitHub Desktop.
open System
open System.IO
module String =
let split chars (s: string) = s.Split chars
/// Lazily returns every file and directory that has the given root path as a parent. Directories paths
/// occur before their children.
let rec getSystemEntriesRec path =
seq { yield path
for subPath in Directory.GetDirectories path do
yield! getSystemEntriesRec subPath
yield! Directory.GetFiles path }
let relativePath root fullPath =
let sep = [|Path.DirectorySeparatorChar|]
fullPath |> String.split sep |> Array.skip ((String.split sep root).Length) |> Path.Combine
let src, dst = "foo", "bar"
src
|> getSystemEntriesRec
|> Seq.map (fun srcFullPath ->
srcFullPath, Path.Combine ([|dst; relativePath src srcFullPath|]))
|> Seq.iter (fun (src, dst) ->
if File.Exists src then File.Copy (src, dst)
else ignore (Directory.CreateDirectory dst))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment