Sample code for 30+ languages & platforms
PowerBuilder

Copy Local File Attributes to a Remote SFTP File

See more SFTP Examples

Demonstrates the Chilkat SFtp.CopyFileAttr method, which copies supported timestamps and attributes from a local file to a remote item. The arguments are the local file, the remote file (a path or an open handle), and isHandle indicating which form the second argument takes.

Note: This example uses a relative local path, which is resolved against the application's current working directory. Absolute local paths may also be used. Supply the path appropriate to your own environment.

Background: An upload transfers a file's bytes but not necessarily its metadata, so the remote copy may end up with the upload time as its modified time. CopyFileAttr propagates the local file's timestamps and attributes to the remote file, which matters for backups and mirrors where preserving modification times is important — and for incremental sync logic that compares timestamps to decide what changed. The isHandle flag lets you target either a remote path or a file you already have open.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Sftp
integer li_Port
string ls_Password
integer li_IsHandle

li_Success = 0

//  Demonstrates the SFtp.CopyFileAttr method, which copies supported timestamps and attributes
//  from a local file to a remote item.  The 1st argument is the local file, the 2nd is the
//  remote file (a path or an open handle), and the 3rd (isHandle) indicates which.

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

//  Upload a file first.
li_Success = loo_Sftp.UploadFileByName("subdir/report.pdf","qa_data/report.pdf")
if li_Success = 0 then
    Write-Debug loo_Sftp.LastErrorText
    destroy loo_Sftp
    return
end if

//  Copy the local file's timestamps and attributes onto the remote file.  Because the 2nd
//  argument is a remote path (not an open handle), isHandle is 0.
li_IsHandle = 0
li_Success = loo_Sftp.CopyFileAttr("qa_data/report.pdf","subdir/report.pdf",li_IsHandle)
if li_Success = 0 then
    Write-Debug loo_Sftp.LastErrorText
    destroy loo_Sftp
    return
end if

Write-Debug "Attributes copied."

loo_Sftp.Disconnect()


destroy loo_Sftp