Visual FoxPro
Visual FoxPro
Check Whether the Last SFTP Read Failed
See more SFTP Examples
Demonstrates the Chilkat SFtp.LastReadFailed method, which returns whether the most recent read for a handle failed. The only argument is the handle. A normal end-of-file is not a failure.
Background: A chunked read loop needs to tell three outcomes apart: more data, a clean end-of-file, and an actual error such as a dropped connection or a revoked handle.
LastReadFailed isolates the error case — it is false on a normal EOF read (which succeeds with zero bytes) and true for an empty, malformed, or already-closed handle — so the loop can stop cleanly on EOF but bail out with an error message on a genuine failure.Chilkat Visual FoxPro Downloads
LOCAL lnSuccess
LOCAL loSftp
LOCAL lnPort
LOCAL lcPassword
LOCAL lcHandle
LOCAL lnReading
LOCAL lnChunkSize
LOCAL loBd
lnSuccess = 0
* Demonstrates the SFtp.LastReadFailed method, which returns whether the most recent read for a
* handle failed. The only argument is the handle. A normal end-of-file is NOT a failure.
loSftp = CreateObject('Chilkat.SFtp')
* Connect, authenticate, and initialize the SFTP subsystem.
lnPort = 22
lnSuccess = loSftp.Connect("sftp.example.com",lnPort)
IF (lnSuccess = 0) THEN
? loSftp.LastErrorText
RELEASE loSftp
CANCEL
ENDIF
* 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.
lcPassword = "mySshPassword"
lnSuccess = loSftp.AuthenticatePw("mySshLogin",lcPassword)
IF (lnSuccess = 0) THEN
? loSftp.LastErrorText
RELEASE loSftp
CANCEL
ENDIF
lnSuccess = loSftp.InitializeSftp()
IF (lnSuccess = 0) THEN
? loSftp.LastErrorText
RELEASE loSftp
CANCEL
ENDIF
lcHandle = loSftp.OpenFile("subdir/data.txt","readOnly","openExisting")
IF (loSftp.LastMethodSuccess = 0) THEN
? loSftp.LastErrorText
RELEASE loSftp
CANCEL
ENDIF
lnReading = 1
lnChunkSize = 4096
loBd = CreateObject('Chilkat.BinData')
DO WHILE lnReading
lnSuccess = loSftp.ReadFileBd(lcHandle,lnChunkSize,loBd)
* Distinguish an actual read failure from a normal EOF. On EOF the read succeeds, returns
* zero bytes, and LastReadFailed is 0.
IF (loSftp.LastReadFailed(lcHandle)) THEN
? "Read failed: " + loSftp.LastErrorText
RELEASE loSftp
RELEASE loBd
CANCEL
ENDIF
lnReading = NOT loSftp.Eof(lcHandle)
ENDDO
lnSuccess = loSftp.CloseHandle(lcHandle)
IF (lnSuccess = 0) THEN
? loSftp.LastErrorText
RELEASE loSftp
RELEASE loBd
CANCEL
ENDIF
? "Read " + STR(loBd.NumBytes) + " bytes with no read failures."
loSftp.Disconnect()
RELEASE loSftp
RELEASE loBd