Sample code for 30+ languages & platforms
Delphi DLL

File Existence Check

See more FTP Examples

Testing to see if a file exists on the FTP server. The GetSizeByName method is a convenient way to check if a file exists. It will return -1 if the file does not exist, otherwise it returns the size of the file in bytes.

Chilkat Delphi DLL Downloads

Delphi DLL
uses
    Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
    Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Ftp2;

...

procedure TForm1.Button1Click(Sender: TObject);
var
success: Boolean;
ftp: HCkFtp2;
fileSize: Integer;

begin
success := False;

// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

ftp := CkFtp2_Create();

CkFtp2_putHostname(ftp,'ftp.example.com');
CkFtp2_putUsername(ftp,'username');
CkFtp2_putPassword(ftp,'password');

// Connect and login to the FTP server.
success := CkFtp2_Connect(ftp);
if (success <> True) then
  begin
    Memo1.Lines.Add(CkFtp2__lastErrorText(ftp));
    Exit;
  end;

// Set the current remote directory to where the file is located:
success := CkFtp2_ChangeRemoteDir(ftp,'something');
if (success <> True) then
  begin
    Memo1.Lines.Add(CkFtp2__lastErrorText(ftp));
    Exit;
  end;

// Test to see if the file exists by getting the file size by name. 
// If a -1 is returned, the file does not exist.
fileSize := CkFtp2_GetSizeByName(ftp,'test123.txt');
if (fileSize < 0) then
  begin
    Memo1.Lines.Add('file does not exist');
  end
else
  begin
    Memo1.Lines.Add('file exists and is ' + IntToStr(fileSize) + ' bytes in size');
  end;

success := CkFtp2_Disconnect(ftp);

CkFtp2_Dispose(ftp);

end;