Created
June 12, 2020 16:38
-
-
Save jannesiera/dab867d47b9b5ae64ae246957f32ed77 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Inspired by : https://docs.microsoft.com/en-us/dotnet/standard/io/how-to-copy-directories | |
// Currently does NOT YET replace files (throws exception if file already exists, should instead remove existing file and overwrite, but be able to recover if something goes wrong during the process) | |
/// Copy and replace the contents from one directory to another. Including subdirectories. | |
let rec copyAndReplaceDirectory sourcePath targetPath : Result<unit list, string list> = | |
let sourceDir = new DirectoryInfo(sourcePath) | |
let targetDir = new DirectoryInfo(targetPath) | |
match sourceDir.Exists, targetDir.Exists with | |
| true, true -> | |
// Directories exist, proceed with copy and replace | |
// Copy all files | |
for file in sourceDir.GetFiles() do | |
file.CopyTo(Path.Combine(targetPath, file.Name)) |> ignore | |
// Recursively copy subdirectories | |
let subdirectoriesResults = | |
[ | |
for subdirectory in sourceDir.GetDirectories() do | |
let subdirectoryTargetPath : string = Path.Combine(targetPath, subdirectory.Name) | |
copyAndReplaceDirectory subdirectory.FullName subdirectoryTargetPath | |
] | |
subdirectoriesResults |> Result.sequence |> Result.mapError List.concat |> Result.map List.concat | |
| true, false -> Error [ ("Failed copy and replace, target directory (" + targetPath + ") doesn't exist") ] | |
| false, true -> Error [ ("Failed copy and replace, Source directory (" + sourcePath + ") doesn't exist") ] | |
| false, false -> Error [ ("Failed copy and replace, target directory (" + targetPath + ") doesn't exist"); ("Failed copy and replace, Source directory (" + sourcePath + ") doesn't exist") ] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment