DataFlex
DataFlex
SFTP Read Text File
See more SFTP Examples
Demonstrates how to open a text file on the SSH server and read text.Chilkat DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoSftp
String sHostname
Integer iPort
String sHandle
String sSText
String sTemp1
Boolean bTemp1
Move False To iSuccess
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
Get Create (RefClass(cComChilkatSFtp)) To hoSftp
If (Not(IsComObjectCreated(hoSftp))) Begin
Send CreateComObject of hoSftp
End
// Set some timeouts, in milliseconds:
Set ComConnectTimeoutMs Of hoSftp To 5000
Set ComIdleTimeoutMs Of hoSftp To 15000
// Connect to the SSH server.
// The standard SSH port = 22
// The hostname may be a hostname or IP address.
Move "sftp.example.com" To sHostname
Move 22 To iPort
Get ComConnect Of hoSftp sHostname iPort To iSuccess
If (iSuccess <> True) Begin
Get ComLastErrorText Of hoSftp To sTemp1
Showln sTemp1
Procedure_Return
End
// Authenticate with the SSH server. Chilkat SFTP supports
// both password-based authenication as well as public-key
// authentication. This example uses password authenication.
Get ComAuthenticatePw Of hoSftp "myLogin" "myPassword" To iSuccess
If (iSuccess <> True) Begin
Get ComLastErrorText Of hoSftp To sTemp1
Showln sTemp1
Procedure_Return
End
// After authenticating, the SFTP subsystem must be initialized:
Get ComInitializeSftp Of hoSftp To iSuccess
If (iSuccess <> True) Begin
Get ComLastErrorText Of hoSftp To sTemp1
Showln sTemp1
Procedure_Return
End
// Open a file for reading.
Get ComOpenFile Of hoSftp "myTest.txt" "readOnly" "openExisting" To sHandle
Get ComLastMethodSuccess Of hoSftp To bTemp1
If (bTemp1 <> True) Begin
Get ComLastErrorText Of hoSftp To sTemp1
Showln sTemp1
Procedure_Return
End
// Assume the file we are reading contains the following text:
// abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ
// (in ANSI format -- i.e. one byte per char).
// Read 26 bytes:
Get ComReadFileText Of hoSftp sHandle 26 "ansi" To sSText
Get ComLastMethodSuccess Of hoSftp To bTemp1
If (bTemp1 <> True) Begin
Get ComLastErrorText Of hoSftp To sTemp1
Showln sTemp1
Procedure_Return
End
// Should print "abcdefghijklmnopqrstuvwxyz"
Showln sSText
// Read the next 10 bytes.
Get ComReadFileText Of hoSftp sHandle 10 "ansi" To sSText
Get ComLastMethodSuccess Of hoSftp To bTemp1
If (bTemp1 <> True) Begin
Get ComLastErrorText Of hoSftp To sTemp1
Showln sTemp1
Procedure_Return
End
// Should print "1234567890"
Showln sSText
// Read the next 26 bytes.
Get ComReadFileText Of hoSftp sHandle 26 "ansi" To sSText
Get ComLastMethodSuccess Of hoSftp To bTemp1
If (bTemp1 <> True) Begin
Get ComLastErrorText Of hoSftp To sTemp1
Showln sTemp1
Procedure_Return
End
// Should print "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
Showln sSText
// Close the file.
Get ComCloseHandle Of hoSftp sHandle To iSuccess
If (iSuccess <> True) Begin
Get ComLastErrorText Of hoSftp To sTemp1
Showln sTemp1
Procedure_Return
End
Showln "Success."
End_Procedure