Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
Check if a Remote File or Directory Exists
See more SFTP Examples
Demonstrates the Chilkat SFtp.FileExists method, which checks a remote path and returns an integer describing what is there. The first argument is the remote path and the second (followLinks) selects whether a symbolic link is followed to its target. The return value is -1 on error, 0 if nothing exists, 1 for a file, 2 for a directory, 3 for a symbolic link, and higher values for special entry types.
Background: This returns more than a yes/no because "does it exist" usually comes with "and is it the kind of thing I expect" — a job that means to write a file should notice if a directory already occupies the path. The
followLinks flag decides whether a symbolic link is reported as a link (3, only when not following) or transparently resolved to its target's type. Always handle the -1 case: it means the check itself failed, which is different from the path not existing.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;
followLinks: Boolean;
result: Integer;
begin
success := False;
// Demonstrates the SFtp.FileExists method, which checks a remote path and returns an integer
// describing what is there. The 1st argument is the remote path and the 2nd (followLinks)
// selects whether a symbolic link is followed to its target.
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;
// When followLinks is True, a symbolic link is followed and the target's type is returned.
followLinks := True;
result := sftp.FileExists('subdir/report.pdf',followLinks);
// Interpret the return value.
if (result = -1) then
begin
WriteLn(sftp.LastErrorText);
Exit;
end;
if (result = 0) then
begin
WriteLn('The path does not exist.');
end;
if (result = 1) then
begin
WriteLn('A regular file exists.');
end;
if (result = 2) then
begin
WriteLn('A directory exists.');
end;
// Other possible values: 3 = symbolic link (only when followLinks is False), 4 = special
// entry, 5 = unknown, 6 = socket, 7 = character device, 8 = block device, 9 = FIFO.
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.