我正在开发RAD Studio(IdFTP
)的FTP客户端。我如何从服务器下载目录? Delphi或C ++。谢谢。
您需要调用TIdFTP.ChangeDir()
转到所需的起始目录,然后调用TIdFTP.List()
来检索其文件和子目录的名称,然后循环访问TIdFTP.DirectoryListing
,在每个文件名上调用TIdFTP.Get()
并将每个子目录名称存储到您自己的本地列表中,然后最后重复在本地列表中的每个子目录上执行上述步骤。
例如:
Procedure DownloadFolder(ARemoteFolder, ALocalFolder: string);
Var
SubFolders: TStringList;
I: Integer;
Begin
ALocalFolder := IncludeTrailingPathDelimiter(ALocalFolder);
ForceDirectories(ALocalFolder);
SubFolders := TStringList.Create;
Try
FTP.ChangeDir(ARemoteFolder);
FTP.List;
For I := 0 to FTP.DirectoryListing.Count-1 do
Begin
If FTP.DirectoryListing[I].ItemType = ditFile then
Begin
FTP.Get(FTP.DirectoryListing[I].FileName, ALocalFolder + FTP.DirectoryListing[I].FileName);
End
Else if FTP.DirectoryListing[I].ItemType = ditDirectory then
Begin
if (FTP.DirectoryListing[I].FileName <> '.') and FTP.DirectoryListing[I].FileName <> '..') then
SubFolders.Add(FTP.DirectoryListing[I].FileName);
End;
End;
For I := 0 to SubFolders.Count-1 do
Begin
DownloadFolder(ARemoteFolder + '/' + SubFolders[I], ALocalFolder + SubFolders[I]);
End;
Finally
SubFolders.Free;
End;
End;
DownloadFolder('/StartingDir', 'C:\Downloaded');
有必要添加条件:
否则if((IdFTP.DirectoryListing [I] .ItemType = ditDirectory)和(Length(IdFTP.DirectoryListing [I] .FileName)> 2))然后
避免“..”作为目录名称