Sample code for 30+ languages & platforms
Pascal (Lazarus/Delphi)

Get the Byte Count of the Last SFTP Read

See more SFTP Examples

Demonstrates the Chilkat SFtp.LastReadNumBytes method, which returns the number of bytes received by the most recent read for a handle. The only argument is the handle.

Background: SFTP reads may return fewer bytes than requested even before the end of the file, so knowing the actual count is useful for tracking progress and for advancing an explicit offset by the right amount. Both a normal EOF read and a failed read report 0, so pair this with Eof and LastReadFailed to interpret a zero-byte result correctly.

Chilkat Pascal (Lazarus/Delphi) Downloads

Pascal (Lazarus/Delphi)
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,
  Chilkat.BinData;

// ---------------------------------------------------------------------------

procedure RunDemo;
var
  success: Boolean;
  sftp: TSFtp;
  port: Integer;
  password: string;
  handle: string;
  bd: TBinData;
  chunkSize: Integer;
  reading: Boolean;
  lastCount: Integer;

begin
  success := False;

  //  Demonstrates the SFtp.LastReadNumBytes method, which returns the number of bytes received by
  //  the most recent read for a handle.  The only argument is the handle.

  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;

  handle := sftp.OpenFile('subdir/data.txt','readOnly','openExisting');
  if (sftp.LastMethodSuccess = False) then
    begin
      WriteLn(sftp.LastErrorText);
      Exit;
    end;

  bd := TBinData.Create;
  chunkSize := 4096;
  reading := True;
  while reading do
    begin
      success := sftp.ReadFileBd(handle,chunkSize,bd);
      if (sftp.LastReadFailed(handle)) then
        begin
          WriteLn(sftp.LastErrorText);
          Exit;
        end;

      //  Report how many bytes the last read actually returned.  A normal EOF read reports 0.
      lastCount := sftp.LastReadNumBytes(handle);
      WriteLn('Last read returned ' + lastCount + ' bytes.');

      reading := not sftp.Eof(handle);
    end;

  success := sftp.CloseHandle(handle);
  if (success = False) then
    begin
      WriteLn(sftp.LastErrorText);
      Exit;
    end;
  WriteLn('Total: ' + bd.NumBytes + ' bytes.');

  sftp.Disconnect();


  sftp.Free;
  bd.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.