DataFlex
DataFlex
Open a Remote File on an SFTP Server
See more SFTP Examples
Demonstrates the Chilkat SFtp.OpenFile method, which opens or creates a remote file and returns a handle for subsequent read or write operations. The arguments are the remote path, the access mode (readOnly, writeOnly, or readWrite), and the create disposition. Close the handle with CloseHandle when finished.
Background: Handle-based access is the foundation for everything the by-name convenience methods hide. The create disposition is the key control — it decides whether a missing file is an error (
openExisting), whether an existing file is truncated (createTruncate) or preserved (openOrCreate), and can add options such as appendData for logging. Because the handle names a server-side resource, it must be closed to avoid exhausting the server's open-file limit.Chilkat DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoSftp
Integer iPort
String sPassword
String sHandle
String sTemp1
Boolean bTemp1
Move False To iSuccess
// Demonstrates the SFtp.OpenFile method, which opens or creates a remote file and returns a
// handle for subsequent read or write operations. The 1st argument is the remote path, the 2nd
// is the access mode, and the 3rd is the create disposition.
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
// access must be "readOnly", "writeOnly", or "readWrite".
//
// createDisposition is a comma-separated list containing exactly one primary disposition:
// createNew Create a new file; fail if it already exists.
// createTruncate Create a new file, or open and truncate an existing file.
// openExisting Open an existing file; fail if it does not exist.
// openOrCreate Open the file if it exists, otherwise create it.
// truncateExisting Open and truncate an existing file; fail if it does not exist.
// Optional additional keywords include appendData, textMode, blockRead, blockWrite, and others.
// Open a remote file for reading.
Get ComOpenFile Of hoSftp "subdir/data.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
Showln "Opened file, handle = " sHandle
// ... read from the handle ...
// Always close the handle when finished.
Get ComCloseHandle Of hoSftp sHandle To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoSftp To sTemp1
Showln sTemp1
Procedure_Return
End
Send ComDisconnect To hoSftp
End_Procedure