简体   繁体   中英

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');

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');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM