My FSharp Notes

My FSharp Notes

My Notes - F#

Basic File Copy with overwrites - but no failsafe if failed.

Can run on Linqpad in F# Expression Mode.

let rec directoryCopy srcPath dstPath copySubDirs =

  if not <| System.IO.Directory.Exists(srcPath) then
      let msg = System.String.Format("Source directory does not exist or could not be found: {0}", srcPath)
      raise (System.IO.DirectoryNotFoundException(msg))

  if not <| System.IO.Directory.Exists(dstPath) then
      System.IO.Directory.CreateDirectory(dstPath) |> ignore

  let srcDir = new System.IO.DirectoryInfo(srcPath)

  for file in srcDir.GetFiles() do
      let temppath = System.IO.Path.Combine(dstPath, file.Name)
      printfn "from:%s -> to:%s" file.FullName temppath
      file.CopyTo(temppath, true) |> ignore

  if copySubDirs then
      for subdir in srcDir.GetDirectories() do
          let dstSubDir = System.IO.Path.Combine(dstPath, subdir.Name)
          directoryCopy subdir.FullName dstSubDir copySubDirs
            
let dirpath = @"D:\directory1"
let remote = @"\\remotefile\directory2"
                      
directoryCopy dirpath remote true

Comments

Popular posts from this blog

Split function with std::List

Welcome to StackEditPro