Sample code for 30+ languages & platforms
PowerBuilder

Rename or Move a Remote File or Directory

See more SFTP Examples

Demonstrates the Chilkat SFtp.RenameFileOrDir method, which renames or moves a remote file or directory. The first argument is the old path and the second is the new path.

Background: Rename and move are the same operation in SFTP: changing only the final path component renames in place, while changing the directory portion relocates the item. When source and destination are on the same filesystem the server does this as a fast metadata change rather than copying data, which is why moving even a large file is near-instant. Moving across filesystems, or onto an existing target, may fail depending on the server.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Sftp
integer li_Port
string ls_Password

li_Success = 0

//  Demonstrates the SFtp.RenameFileOrDir method, which renames or moves a remote file or
//  directory.  The 1st argument is the old path and the 2nd is the new path.

loo_Sftp = create oleobject
li_rc = loo_Sftp.ConnectToNewObject("Chilkat.SFtp")
if li_rc < 0 then
    destroy loo_Sftp
    MessageBox("Error","Connecting to COM object failed")
    return
end if

//  Connect, authenticate, and initialize the SFTP subsystem.
li_Port = 22
li_Success = loo_Sftp.Connect("sftp.example.com",li_Port)
if li_Success = 0 then
    Write-Debug loo_Sftp.LastErrorText
    destroy loo_Sftp
    return
end if

//  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.
ls_Password = "mySshPassword"

li_Success = loo_Sftp.AuthenticatePw("mySshLogin",ls_Password)
if li_Success = 0 then
    Write-Debug loo_Sftp.LastErrorText
    destroy loo_Sftp
    return
end if

li_Success = loo_Sftp.InitializeSftp()
if li_Success = 0 then
    Write-Debug loo_Sftp.LastErrorText
    destroy loo_Sftp
    return
end if

//  Rename a file within the same directory.
li_Success = loo_Sftp.RenameFileOrDir("subdir/report.txt","subdir/report_final.txt")
if li_Success = 0 then
    Write-Debug loo_Sftp.LastErrorText
    destroy loo_Sftp
    return
end if

//  Because rename can also move, the same method relocates an item to a different directory.
li_Success = loo_Sftp.RenameFileOrDir("subdir/report_final.txt","archive/report_final.txt")
if li_Success = 0 then
    Write-Debug loo_Sftp.LastErrorText
    destroy loo_Sftp
    return
end if

Write-Debug "Renamed and moved."

loo_Sftp.Disconnect()


destroy loo_Sftp