Tcl
Tcl
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 Tcl Downloads
load ./chilkat.dll
set success 0
# Demonstrates the SFtp.RemoveDir method, which deletes a remote directory. The only argument
# is the directory path.
set sftp [new_CkSFtp]
# Connect, authenticate, and initialize the SFTP subsystem.
set port 22
set success [CkSFtp_Connect $sftp "sftp.example.com" $port]
if {$success == 0} then {
puts [CkSFtp_lastErrorText $sftp]
delete_CkSFtp $sftp
exit
}
# 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.
set password "mySshPassword"
set success [CkSFtp_AuthenticatePw $sftp "mySshLogin" $password]
if {$success == 0} then {
puts [CkSFtp_lastErrorText $sftp]
delete_CkSFtp $sftp
exit
}
set success [CkSFtp_InitializeSftp $sftp]
if {$success == 0} then {
puts [CkSFtp_lastErrorText $sftp]
delete_CkSFtp $sftp
exit
}
# Delete a remote directory. Most SFTP servers require the directory to be empty first.
set success [CkSFtp_RemoveDir $sftp "subdir/oldfolder"]
if {$success == 0} then {
puts [CkSFtp_lastErrorText $sftp]
delete_CkSFtp $sftp
exit
}
puts "Directory removed."
CkSFtp_Disconnect $sftp
delete_CkSFtp $sftp