Delphi和IdFtp - 如何将所有文件上传到目录中

时间:2017-06-11 17:53:16

标签: delphi ftp

我在目录中有几个xml文件,但我只能按文件发送。我想发送该目录中的所有文件。我怎么能这样做?

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

1 个答案:

答案 0 :(得分:5)

此时Indy没有实现任何种类的多重放置方法(FTP协议本身没有这样的功能)。您必须列出给定目录中的所有文件,并分别为每个文件调用Put。例如:

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;

或类似于最近的Delphi版本:

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;

并致电:

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