DataFlex
DataFlex
Remove a Remote Directory on an SFTP Server
See more SFTP Examples
Demonstrates the Chilkat SFtp.RemoveDir method, which deletes a remote directory. The only argument is the directory path.
Background: Most SFTP servers refuse to remove a directory that is not empty, so deleting a populated tree means removing its files and subdirectories first — the inverse of building a path level by level. There is no single "recursive delete" in the SFTP protocol; a client implements it by listing, descending, and removing bottom-up.
Chilkat DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoSftp
Integer iPort
String sPassword
String sTemp1
Move False To iSuccess
// Demonstrates the SFtp.RemoveDir method, which deletes a remote directory. The only argument
// is the directory path.
Get Create (RefClass(cComChilkatSFtp)) To hoSftp
If (Not(IsComObjectCreated(hoSftp))) Begin
Send CreateComObject of hoSftp
End
// Connect, authenticate, and initialize the SFTP subsystem.
Move 22 To iPort
Get ComConnect Of hoSftp "sftp.example.com" iPort To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoSftp To sTemp1
Showln sTemp1
Procedure_Return
End
// Normally you would not hard-code the password in source. You should instead obtain it
// from an interactive prompt, environment variable, or a secrets vault.
Move "mySshPassword" To sPassword
Get ComAuthenticatePw Of hoSftp "mySshLogin" sPassword To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoSftp To sTemp1
Showln sTemp1
Procedure_Return
End
Get ComInitializeSftp Of hoSftp To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoSftp To sTemp1
Showln sTemp1
Procedure_Return
End
// Delete a remote directory. Most SFTP servers require the directory to be empty first.
Get ComRemoveDir Of hoSftp "subdir/oldfolder" To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoSftp To sTemp1
Showln sTemp1
Procedure_Return
End
Showln "Directory removed."
Send ComDisconnect To hoSftp
End_Procedure