Sample code for 30+ languages & platforms
Visual FoxPro

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 Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL loSftp
LOCAL lcHandle

lnSuccess = 0

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

loSftp = CreateObject('Chilkat.SFtp')

* Pass a domain or IP address..
lnSuccess = loSftp.Connect("my-sftp-server.com",22)
IF (lnSuccess = 1) THEN
    lnSuccess = loSftp.AuthenticatePw("mySFtpLogin","mySFtpPassword")
ENDIF

IF (lnSuccess = 1) THEN
    lnSuccess = loSftp.InitializeSftp()
ENDIF

IF (lnSuccess <> 1) THEN
    ? loSftp.LastErrorText
    RELEASE loSftp
    CANCEL
ENDIF

* Open a file on the server for writing.
lcHandle = loSftp.OpenFile("myTest.txt","writeOnly","createTruncate")
IF (loSftp.LastMethodSuccess <> 1) THEN
    ? loSftp.LastErrorText
    RELEASE loSftp
    CANCEL
ENDIF

* Write some text to the file:
lnSuccess = loSftp.WriteFileText(lcHandle,"ansi","abcdefghijklmnopqrstuvwxyz")
IF (lnSuccess <> 1) THEN
    ? loSftp.LastErrorText
    RELEASE loSftp
    CANCEL
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)
lnSuccess = loSftp.Fsync(lcHandle)
IF (lnSuccess <> 1) THEN
    ? loSftp.LastErrorText
    RELEASE loSftp
    CANCEL
ENDIF

* ....
* ....

lnSuccess = loSftp.WriteFileText(lcHandle,"ansi","ABCDEFGHIJKLMNOPQRSTUVWXYZ")
IF (lnSuccess <> 1) THEN
    ? loSftp.LastErrorText
    RELEASE loSftp
    CANCEL
ENDIF

* Close the file.
lnSuccess = loSftp.CloseHandle(lcHandle)
IF (lnSuccess <> 1) THEN
    ? loSftp.LastErrorText
    RELEASE loSftp
    CANCEL
ENDIF

? "Success."

RELEASE loSftp