Sample code for 30+ languages & platforms
PowerBuilder

Flush an SFTP File to Stable Storage (Fsync)

See more SFTP Examples

Demonstrates the Chilkat SFtp.Fsync method, which requests that the server flush pending data for an open file to stable storage. The only argument is the handle. It uses the OpenSSH fsync@openssh.com extension and succeeds only when the server supports it.

Background: A successful write does not guarantee the data has reached disk — it may still sit in the server's write cache, where a crash or power loss would lose it. Fsync forces it to durable storage, which matters for transactional or journal-style files where a committed record must survive a failure. Because it relies on a non-standard OpenSSH extension, expect it to fail against servers that do not implement it, and treat that as a capability check rather than a hard error where appropriate.

Chilkat PowerBuilder Downloads

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

li_Success = 0

//  Demonstrates the SFtp.Fsync method, which requests that the server flush pending data for an
//  open file to stable storage.  The only argument is the handle.

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

ls_Handle = loo_Sftp.OpenFile("subdir/journal.log","writeOnly","openOrCreate,appendData")
if loo_Sftp.LastMethodSuccess = 0 then
    Write-Debug loo_Sftp.LastErrorText
    destroy loo_Sftp
    return
end if

//  Write a critical record.
li_Success = loo_Sftp.WriteFileText(ls_Handle,"utf-8","2026-07-23 transaction committed~n")
if li_Success = 0 then
    Write-Debug loo_Sftp.LastErrorText
    destroy loo_Sftp
    return
end if

//  Ask the server to flush the data to durable storage before continuing.  Fsync uses the
//  OpenSSH fsync@openssh.com extension and succeeds only when the server supports it.
li_Success = loo_Sftp.Fsync(ls_Handle)
if li_Success = 0 then
    Write-Debug loo_Sftp.LastErrorText
    destroy loo_Sftp
    return
end if

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

Write-Debug "Data flushed to stable storage."

loo_Sftp.Disconnect()


destroy loo_Sftp