Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
Write Text to a Remote SFTP File (Sequential)
See more SFTP Examples
Demonstrates the Chilkat SFtp.WriteFileText method, which encodes text using a charset and writes it at the current sequential write position of an open handle. The arguments are the handle, the charset, and the text.
Background: Chilkat tracks a write position per handle, so successive
WriteFileText calls append naturally — the pattern for building a file a piece at a time, such as writing rows to a report. Opening with openOrCreate starts writing at the current end of the file. The charset determines the bytes actually stored, which matters whenever the text contains non-ASCII characters.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.WriteFileText method, which encodes text and writes it at the current
// sequential write position of an open handle. The 1st argument is the handle, the 2nd is the
// charset, and the 3rd is the text.
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;
// Open (creating/truncating) the remote file for writing.
handle := sftp.OpenFile('subdir/notes.txt','writeOnly','createTruncate');
if (sftp.LastMethodSuccess = False) then
begin
WriteLn(sftp.LastErrorText);
Exit;
end;
// Write text as UTF-8. Successive writes continue from where the previous write ended.
success := sftp.WriteFileText(handle,'utf-8','First line.' + #10);
if (success = False) then
begin
WriteLn(sftp.LastErrorText);
Exit;
end;
success := sftp.WriteFileText(handle,'utf-8','Second line.' + #10);
if (success = False) then
begin
WriteLn(sftp.LastErrorText);
Exit;
end;
success := sftp.CloseHandle(handle);
if (success = False) then
begin
WriteLn(sftp.LastErrorText);
Exit;
end;
WriteLn('Wrote text file.');
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.