在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
如何用idftp遍历整个目录----下载、删除_delphi教程 unit Unit1; interface uses type var implementation {$R *.dfm} { 下载整个目录,并遍历所有子目录 首先 ChangeDir(Root) 到根目录 然后创建本地目录 + RemoteDir 然后用 list 得到所有目录名 循环判断,进入 RemoteDir 目录内部 如果是目录继续第归。否则 get 该文件到本地目录,当 get 完所有文件后返回上一级目录 用List再取得信息,继续循环 } procedure FTP_DownloadDir(var idFTP : TIdFtp;RemoteDir,LocalDir : string); label Files ; var i,DirCount : integer; begin if not DirectoryExists(LocalDir + RemoteDir) then ForceDirectories(LocalDir + RemoteDir); idFTP.ChangeDir(RemoteDir); idFTP.List(nil); DirCount := idFTP.DirectoryListing.Count ; if DirCount = 0 then begin idFTP.ChangeDirUp; idFTP.List(nil); end; for i := 0 to DirCount - 1 do begin if DirCount <> idFTP.DirectoryListing.Count then begin repeat idFTP.ChangeDirUp; idFTP.List(nil); until DirCount = idFTP.DirectoryListing.Count ; end; if idFTP.DirectoryListing[i].ItemType = ditDirectory then FTP_DownloadDir(idFTP,idFTP.DirectoryListing[i].FileName,LocalDir + RemoteDir + \) else begin idFTP.Get(idFTP.DirectoryListing[i].FileName,LocalDir + RemoteDir + \ + idFTP.DirectoryListing[i].FileName,true); Form1.lb_num.Caption := IntToStr(StrToInt(Form1.lb_num.Caption) + 1); Form1.lb_num.Update; if i = DirCount - 1 then begin idFTP.ChangeDirUp; idFTP.List(nil); end; end; end; end; {删除整个ftp目录,包括下面的文件, RootDir = 要删除的根目录,一般情况下 RemoteDir 与 RootDir 相等} procedure FTP_DeleteAllFiles(var idFTP : TIdFtp;RemoteDir,RootDir : string); label Files; var i,DirCount : integer; Temp : string; begin idFTP.ChangeDir(RemoteDir); if Pos(RootDir,idFTP.RetrieveCurrentDir) = 0 then Exit; Files : idFTP.List(nil); DirCount := idFTP.DirectoryListing.Count ; while DirCount = 0 do begin Temp := idFTP.RetrieveCurrentDir; idFTP.ChangeDirUp; idFTP.RemoveDir(Temp); idFTP.List(nil); DirCount := idFTP.DirectoryListing.Count ; for i := 0 to DirCount - 1 do if idFTP.DirectoryListing[i].FileName = RootDir then Exit; end; for i := 0 to DirCount - 1 do begin if Pos(RootDir,idFTP.RetrieveCurrentDir) = 0 then Break ; if idFTP.DirectoryListing[i].ItemType = ditDirectory then begin FTP_DeleteAllFiles(idFTP,idFTP.DirectoryListing[i].FileName,RootDir); end else begin idFTP.Delete(idFTP.DirectoryListing[i].FileName); Form1.lb_num.Caption := IntToStr(StrToInt(Form1.lb_num.Caption) + 1); Form1.lb_num.Update; goto Files ; end; end; end; procedure TForm1.Btt_DownLoadDirClick(Sender: TObject); begin IdFTP1.Connect(true,-1); if IdFTP1.Connected then begin IdFTP1.ChangeDir(bigimage); FTP_DownloadDir(IdFTP1,1002.1002.1002,g:\ftpdir\); end; IdFTP1.Disconnect ; end; procedure TForm1.Btt_DeleteDirClick(Sender: TObject); begin IdFTP1.Connect(true,-1); if IdFTP1.Connected then begin IdFTP1.ChangeDir(bigimage); FTP_DeleteAllFiles(IdFTP1,1002.1002.1002,1002.1002.1002); end; IdFTP1.Disconnect ; end; end. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论