unit uFTPClient;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, IdBaseComponent, IdComponent,
  IdTCPConnection, IdTCPClient, IdExplicitTLSClientServerBase, IdFTP,
  Vcl.StdCtrls, IdAntiFreezeBase, Vcl.IdAntiFreeze;

type
  TForm2 = class(TForm)
    IdFTP: TIdFTP;
    bConnect: TButton;
    OpenDialog: TOpenDialog;
    Memo1: TMemo;
    IdAntiFreeze1: TIdAntiFreeze;
    bTest: TButton;
    bPutFile: TButton;
    bChangeDir: TButton;
    Edit1: TEdit;
    procedure bConnectClick(Sender: TObject);
    procedure IdFTPWorkEnd(ASender: TObject; AWorkMode: TWorkMode);
    procedure bTestClick(Sender: TObject);
    procedure bPutFileClick(Sender: TObject);
    procedure bChangeDirClick(Sender: TObject);
    procedure IdFTPAfterPut(Sender: TObject);
  private

    FFileToPut:string;

    function IsSuchDirOnFTPServer(dirname:string):boolean;
    function IsSuchFileOnFTPServer(filename: string): boolean;
    procedure FillMemo;
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form2: TForm2;

implementation

{$R *.dfm}

procedure TForm2.bChangeDirClick(Sender: TObject);
begin
IdFTP.ChangeDir(Edit1.Text);

IdFTP.List;
FillMemo;

end;

procedure TForm2.bConnectClick(Sender: TObject);
var SL:TStringList;
  i: Integer;
begin
idFTP.Host:='localhost';   //FTP-сервер
idFTP.Port:=21;                  //порт ФТП сервера
idFTP.Username:='Логин';
idFTP.Password:='Пароль';
idFTP.Connect;


if idFTP.Connected then Caption:='Connected';


idFTP.List; // << Не дружит с Касперским

// Заполняем Memo
Memo1.Clear;
//for i := 0 to IdFTP.ListResult.Count-1 do
//Memo1.Lines.Add(IdFTP.ListResult[i]);

for I := 0 to IdFTP.DirectoryListing.Count-1 do
Memo1.Lines.Add(IdFTP.DirectoryListing[i].FileName);




end;

procedure TForm2.bPutFileClick(Sender: TObject);
begin


idFTP.MakeDir('/files');
IdFTP.ChangeDir('/files');


if OpenDialog.Execute then
 begin
 FFileToPut:=OpenDialog.FileName;
 idFTP.Put(OpenDialog.FileName);
// idFTP.Disconnect;

 end;


end;

procedure TForm2.bTestClick(Sender: TObject);
begin

ShowMessage(
IdFTP.RetrieveCurrentDir
);





end;

procedure TForm2.IdFTPAfterPut(Sender: TObject);
begin
//ShowMessage('After Put');
//ShowMessage(IdFTP.RetrieveCurrentDir);
IdFTP.List;

FillMemo;

if IsSuchFileOnFTPServer(FFileToPut) then ShowMessage('Ok');

end;

procedure TForm2.IdFTPWorkEnd(ASender: TObject; AWorkMode: TWorkMode);
begin

//IdFTP.List;
//if IsSuchFileOnFTPServer(FFileToPut) then ShowMessage('Ok');

end;

function TForm2.IsSuchDirOnFTPServer(dirname:string):boolean;
var
  i: Integer;
  s: string;
begin



{
//  IdFTP.List;
  //Ищем - есть ли такая директория
  for i := 0 to IdFTP.ListResult.Count - 1 do
  begin
    s := IdFTP.ListResult[i];
    if (Pos('type=dir', s) <> 0) and (Pos(dirname, s) <> 0) then
      ShowMessage('Да, такая директория есть');
  end;

}
end;


function TForm2.IsSuchFileOnFTPServer(filename:string):boolean;
var
  i: Integer;
  s: string;
begin

//IdFTP.ChangeDir('/files');

filename:=ExtractFileName(filename);

Result:=False;

for i := 0 to IdFTP.DirectoryListing.Count - 1 do
 begin

 if IdFTP.DirectoryListing[i].FileName=filename then
 Result:=true;

 end;

{
  //Ищем - есть ли такая директория
  for i := 0 to IdFTP.ListResult.Count - 1 do
  begin
    s := IdFTP.ListResult[i];
    if (Pos('type=file', s) <> 0) and (Pos(filename, s) <> 0) then
      ShowMessage('Да, такая директория есть');
  end;

}

end;

procedure TForm2.FillMemo;
var
  I: Integer;
begin
  // Заполняем Memo
  Memo1.Clear;
  for I := 0 to IdFTP.DirectoryListing.Count - 1 do
    Memo1.Lines.Add(IdFTP.DirectoryListing[i].FileName);
  ShowMessage(IdFTP.RetrieveCurrentDir);
end;



end.
