Sample code for 30+ languages & platforms
Visual FoxPro

Read Text from a Remote SFTP File (Sequential)

See more SFTP Examples

Demonstrates the Chilkat SFtp.ReadFileText method, which reads up to a number of bytes from the current position of an open handle and decodes them to text. The arguments are the handle, the maximum number of bytes, and the charset. This example reads in chunks until end-of-file.

Background: Reading a file in bounded chunks keeps memory use predictable regardless of file size, which is why the loop reads a fixed amount and repeats until Eof. A subtlety worth knowing: the byte limit is applied to the encoded bytes, so a chunk boundary could in principle fall in the middle of a multibyte character — accumulating the pieces and decoding the whole avoids any such issue in practice here since the text is reassembled in a StringBuilder.

Chilkat Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL loSftp
LOCAL lnPort
LOCAL lcPassword
LOCAL lcHandle
LOCAL loSbContent
LOCAL lnChunkSize
LOCAL lnReading
LOCAL lcChunk

lnSuccess = 0

*  Demonstrates the SFtp.ReadFileText method, which reads up to a number of bytes from the current
*  position of an open handle and decodes them to text.  The 1st argument is the handle, the 2nd
*  is the maximum number of bytes, and the 3rd is the charset.

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

*  Open the remote file for reading.
lcHandle = loSftp.OpenFile("subdir/notes.txt","readOnly","openExisting")
IF (loSftp.LastMethodSuccess = 0) THEN
    ? loSftp.LastErrorText
    RELEASE loSftp
    CANCEL
ENDIF

*  Read the file in chunks until end-of-file, accumulating the text.
loSbContent = CreateObject('Chilkat.StringBuilder')
lnChunkSize = 4096
lnReading = 1
DO WHILE lnReading
    lcChunk = loSftp.ReadFileText(lcHandle,lnChunkSize,"utf-8")
    IF (loSftp.LastReadFailed(lcHandle)) THEN
        ? loSftp.LastErrorText
        RELEASE loSftp
        RELEASE loSbContent
        CANCEL
    ENDIF

    loSbContent.Append(lcChunk)

    *  Stop once the end of the file has been reached.
    lnReading = NOT loSftp.Eof(lcHandle)
ENDDO

lnSuccess = loSftp.CloseHandle(lcHandle)
IF (lnSuccess = 0) THEN
    ? loSftp.LastErrorText
    RELEASE loSftp
    RELEASE loSbContent
    CANCEL
ENDIF

? loSbContent.GetAsString()

loSftp.Disconnect()

RELEASE loSftp
RELEASE loSbContent