您的位置:首页 > 编程语言 > PHP开发

Downloading directory from FTP server

2016-02-26 16:32 627 查看
You need to call
TIdFTP.ChangeDir()
to go to the desired starting directory, then call
TIdFTP.List()
to retrieve the names of its files and subdirectories, then loop through the
TIdFTP.DirectoryListing
calling
TIdFTP.Get()
on each filename and store each subdirectory name into your own local list, then finally repeat the above steps on each subdirectory in your local list.

For example:

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


Usage:

DownloadFolder('/StartingDir', 'C:\Downloaded');
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: