V.Salles
V.Salles

Reputation: 403

Delphi and IdFtp - How to upload all files into directory

I have several xml files inside a directory, but I can only send file by file. I would like to send all the files that are inside that directory. How can I do this?

idftp1.Put('C:\MyDir\*.xml','/xml/*.xml');

Upvotes: 3

Views: 4518

Answers (1)

Victoria
Victoria

Reputation: 7912

Indy does not implement any kind of multiple put method at this time (the FTP protocol itself does not have such feature). You will have to list all the files in a given directory and call Put for each file separately. For example:

procedure GetFileList(const Folder, Filter: string; FileList: TStrings);
var
  Search: TSearchRec;
begin
  if FindFirst(Folder + Filter, faAnyfile, Search) = 0 then
  try
    FileList.BeginUpdate;
    try
      repeat
        if (Search.Attr and faDirectory <> faDirectory) then
          FileList.Add(Search.Name);
      until
        FindNext(Search) <> 0;
    finally
      FileList.EndUpdate;
    end;
  finally
    FindClose(Search);
  end;
end;

procedure MultiStor(FTP: TIdFTP; const Folder: string; const Filter: string = '*.*');
var
  I: Integer;
  FileList: TStrings;
begin
  FileList := TStringList.Create;
  try
    GetFileList(Folder, Filter, FileList);
    for I := 0 to FileList.Count-1 do
      FTP.Put(Folder + FileList[I]);
  finally
    FileList.Free;
  end;
end;

Or similar for recent Delphi versions:

procedure MultiStor(FTP: TIdFTP; const Folder: string; const Filter: string = '*.*');
var
  FileName: string;
begin
  for FileName in TDirectory.GetFiles(Folder, Filter) do
    FTP.Put(Folder + FileName);
end;

And its call:

MultiStor(IdFTP1, 'C:\MyFolder\', '*.xml');

Upvotes: 8

Related Questions