Sample code for 30+ languages & platforms
Xbase++

SFTP Fsync -- Flush an Open File on the Server

See more SFTP Examples

Demonstrates how to flush the contents of an open file on the server. This example only works for servers that implement the fsync@openssh.com extension.

Note: This example requires Chilkat v9.5.0.71 or later.

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oSftp
LOCAL cHandle

nSuccess := 0

//  This example assumes the Chilkat API to have been previously unlocked.
//  See Global Unlock Sample for sample code.

oSftp := CreateObject("Chilkat.SFtp")

//  Pass a domain or IP address..
nSuccess := oSftp:Connect("my-sftp-server.com", 22)
IF (nSuccess == 1)
    nSuccess := oSftp:AuthenticatePw("mySFtpLogin", "mySFtpPassword")
ENDIF

IF (nSuccess == 1)
    nSuccess := oSftp:InitializeSftp()
ENDIF

IF (nSuccess != 1)
    ? oSftp:LastErrorText
    oSftp:destroy()
    RETURN
ENDIF

//  Open a file on the server for writing.
cHandle := oSftp:OpenFile("myTest.txt", "writeOnly", "createTruncate")
IF (oSftp:LastMethodSuccess != 1)
    ? oSftp:LastErrorText
    oSftp:destroy()
    RETURN
ENDIF

//  Write some text to the file:
nSuccess := oSftp:WriteFileText(cHandle, "ansi", "abcdefghijklmnopqrstuvwxyz")
IF (nSuccess != 1)
    ? oSftp:LastErrorText
    oSftp:destroy()
    RETURN
ENDIF

//  Make sure the server flushes what we wrote to the disk..
//  (this is requires a server that implements the fsync@openssh.com SFTP protocol extension)
nSuccess := oSftp:Fsync(cHandle)
IF (nSuccess != 1)
    ? oSftp:LastErrorText
    oSftp:destroy()
    RETURN
ENDIF

//  ....
//  ....

nSuccess := oSftp:WriteFileText(cHandle, "ansi", "ABCDEFGHIJKLMNOPQRSTUVWXYZ")
IF (nSuccess != 1)
    ? oSftp:LastErrorText
    oSftp:destroy()
    RETURN
ENDIF

//  Close the file.
nSuccess := oSftp:CloseHandle(cHandle)
IF (nSuccess != 1)
    ? oSftp:LastErrorText
    oSftp:destroy()
    RETURN
ENDIF

? "Success."

oSftp:destroy()