Skip to content

Commit

Permalink
Merge pull request #1018 from bogdangaliceanu/FtpDelete
Browse files Browse the repository at this point in the history
add functionality for deleting files and folders to FTP Helper
  • Loading branch information
forki committed Nov 30, 2015
2 parents 2dfd40f + 196059d commit 64b7e6d
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions src/app/FakeLib/FtpHelper.fs
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,54 @@ and private upload server user pwd (fsi : FileSystemInfo) (rootDir : string) =
uploadAFolder server user pwd fsi.FullName (sprintf "%s\\%s" rootDir fsi.Name)
| "System.IO.FileInfo" -> uploadAFile server user pwd (sprintf "%s\\%s" rootDir fsi.Name) fsi.FullName
| _ -> logfn "Unknown object found at %A" fsi

/// Deletes a single file from remote FTP folder.
/// ## Parameters
/// - `destPath` - The full path to the file which needs to be deleted, including all its parent folders
/// - `server` - FTP Server name (ex: "ftp://10.100.200.300:21/")
/// - `user` - FTP Server login name (ex: "joebloggs")
/// - `pwd` - FTP Server login password (ex: "J0Eblogg5")
let deleteAFile (server : string) (user : string) (pwd : string) (destPath : string) =
logfn "Deleting %s" destPath
destPath
|> fun p -> getServerInfo (sprintf "%s/%s" server p) user pwd WebRequestMethods.Ftp.DeleteFile
|> fun si ->
use response = (si.Request.GetResponse() :?> FtpWebResponse)
logfn "Delete file %s status: %s" destPath response.StatusDescription

let private getFolderContents (server : string) (user : string) (pwd : string) (destPath : string) =
getServerInfo (sprintf "%s/%s" server destPath) user pwd WebRequestMethods.Ftp.ListDirectory
|> fun si ->
use response = (si.Request.GetResponse() :?> FtpWebResponse)
use responseStream = response.GetResponseStream()
use reader = new StreamReader(responseStream)
[ while not reader.EndOfStream do yield reader.ReadLine() ]

let private deleteEmptyFolder (server : string) (user : string) (pwd : string) (destPath : string) =
destPath
|> fun p -> getServerInfo (sprintf "%s/%s" server p) user pwd WebRequestMethods.Ftp.RemoveDirectory
|> fun si ->
use response = (si.Request.GetResponse() :?> FtpWebResponse)
logfn "Delete folder %s status: %s" destPath response.StatusDescription

/// Deletes a single folder from remote FTP folder.
/// ## Parameters
/// - `destPath` - The full path to the folder which needs to be deleted, including all its parent folders
/// - `server` - FTP Server name (ex: "ftp://10.100.200.300:21/")
/// - `user` - FTP Server login name (ex: "joebloggs")
/// - `pwd` - FTP Server login password (ex: "J0Eblogg5")
let rec deleteAFolder (server : string) (user : string) (pwd : string) (destPath : string) =
logfn "Deleting %s" destPath
let folderContents = getFolderContents server user pwd destPath

if folderContents |> List.isEmpty then
deleteEmptyFolder server user pwd destPath
else
folderContents
|> List.iter (fun entry ->
try
deleteAFile server user pwd (Path.Combine(destPath, entry))
with
| _ -> deleteAFolder server user pwd (Path.Combine(destPath, entry)))

deleteEmptyFolder server user pwd destPath

0 comments on commit 64b7e6d

Please sign in to comment.