Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
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 Pascal (Lazarus/Delphi) Downloads
program ChilkatDemo;
// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.
{$IFDEF FPC}
{$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}
uses
{$IFDEF UNIX}
cthreads,
{$ENDIF}
SysUtils,
CkDllLoader,
Chilkat.SFtp;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
sftp: TSFtp;
port: Integer;
password: string;
handle: string;
begin
success := False;
// 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.
sftp := TSFtp.Create;
// Connect, authenticate, and initialize the SFTP subsystem.
port := 22;
success := sftp.Connect('sftp.example.com',port);
if (success = False) then
begin
WriteLn(sftp.LastErrorText);
Exit;
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.
password := 'mySshPassword';
success := sftp.AuthenticatePw('mySshLogin',password);
if (success = False) then
begin
WriteLn(sftp.LastErrorText);
Exit;
end;
success := sftp.InitializeSftp();
if (success = False) then
begin
WriteLn(sftp.LastErrorText);
Exit;
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.
handle := sftp.OpenFile('subdir/data.txt','readOnly','openExisting');
if (sftp.LastMethodSuccess = False) then
begin
WriteLn(sftp.LastErrorText);
Exit;
end;
WriteLn('Opened file, handle = ' + handle);
// ... read from the handle ...
// Always close the handle when finished.
success := sftp.CloseHandle(handle);
if (success = False) then
begin
WriteLn(sftp.LastErrorText);
Exit;
end;
sftp.Disconnect();
sftp.Free;
end;
// ---------------------------------------------------------------------------
begin
try
RunDemo;
except
on E: Exception do
WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
end;
WriteLn;
{$IFDEF MSWINDOWS}
WriteLn('Press Enter to exit...');
ReadLn;
{$ENDIF}
end.