我正在通过 RAD Studio ( IdFTP
) 开发 FTP 客户端。如何从服务器下载目录?德尔福或 C++。谢谢。
问问题
13101 次
2 回答
9
你需要调用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');
于 2014-04-18T16:24:59.513 回答
2
需要添加条件:
否则如果 ((IdFTP.DirectoryListing[I].ItemType = ditDirectory) 和 (Length(IdFTP.DirectoryListing[I].FileName) > 2) ) 那么
避免“..”作为目录名
于 2017-11-14T06:04:37.203 回答