DataFlex
DataFlex
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 DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoSftp
Integer iPort
String sPassword
String sHandle
Handle hoSbContent
Integer iChunkSize
Boolean iReading
String sChunk
String sTemp1
Boolean bTemp1
Move False To iSuccess
// 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.
Get Create (RefClass(cComChilkatSFtp)) To hoSftp
If (Not(IsComObjectCreated(hoSftp))) Begin
Send CreateComObject of hoSftp
End
// Connect, authenticate, and initialize the SFTP subsystem.
Move 22 To iPort
Get ComConnect Of hoSftp "sftp.example.com" iPort To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoSftp To sTemp1
Showln sTemp1
Procedure_Return
End
// 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.
Move "mySshPassword" To sPassword
Get ComAuthenticatePw Of hoSftp "mySshLogin" sPassword To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoSftp To sTemp1
Showln sTemp1
Procedure_Return
End
Get ComInitializeSftp Of hoSftp To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoSftp To sTemp1
Showln sTemp1
Procedure_Return
End
// Open the remote file for reading.
Get ComOpenFile Of hoSftp "subdir/notes.txt" "readOnly" "openExisting" To sHandle
Get ComLastMethodSuccess Of hoSftp To bTemp1
If (bTemp1 = False) Begin
Get ComLastErrorText Of hoSftp To sTemp1
Showln sTemp1
Procedure_Return
End
// Read the file in chunks until end-of-file, accumulating the text.
Get Create (RefClass(cComChilkatStringBuilder)) To hoSbContent
If (Not(IsComObjectCreated(hoSbContent))) Begin
Send CreateComObject of hoSbContent
End
Move 4096 To iChunkSize
Move True To iReading
While iReading
Get ComReadFileText Of hoSftp sHandle iChunkSize "utf-8" To sChunk
Get ComLastReadFailed Of hoSftp sHandle To bTemp1
If (bTemp1) Begin
Get ComLastErrorText Of hoSftp To sTemp1
Showln sTemp1
Procedure_Return
End
Get ComAppend Of hoSbContent sChunk To iSuccess
// Stop once the end of the file has been reached.
Get ComEof Of hoSftp sHandle To bTemp1
Move (Not bTemp1) To iReading
Loop
Get ComCloseHandle Of hoSftp sHandle To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoSftp To sTemp1
Showln sTemp1
Procedure_Return
End
Get ComGetAsString Of hoSbContent To sTemp1
Showln sTemp1
Send ComDisconnect To hoSftp
End_Procedure