国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

如何用idFTP遍歷整個目錄----下載、刪除

2019-11-18 18:24:05
字體:
來源:轉載
供稿:網友

如何用idFTP遍歷整個目錄下載、刪除

 

好久不在網上發表文章了,主要因為水平太臭,恐怕耽誤了各位兄弟姐妹的前程,哈哈!

廢話少說,下面切入正題。

       這兩天做一個項目,其中需要用ftp下載服務器上的整個目錄,并且下載完成后,刪除整個目錄。由于ftp不能穿透子目錄,只能在當前目錄下操作,所以用一般的方法根本無法達到預期效果。可能我想偷懶吧!于是想從網上搜搜,看有沒有現成的東東拿來使用 :)

結果令我非常失望,不是無法運行就是達不到我的預期效果。其實論壇上也有人問過這樣的問題,可一直也沒有滿意的結果。哎!還得靠自己呀!小日本可沒有那么聽話,不知道大家聽沒聽說釣魚島,去沒去參加游行。

       下面的程序是用delphi7.0 + idFTP 實現的。可能還會有bug,不過希望能給需要他的人帶來一點點幫助和提示!,程序中只有下載與刪除的代碼,至于上傳的code自己寫吧,稍微思考一下就可以實現。

unit Unit1;

 

interface

 

uses

  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

  Dialogs, StdCtrls, IdBaseComponent, IdComponent, IdTCPConnection,IdFTPList,

  IdTCPClient, IdFTP ;

 

type

  TForm1 = class(TForm)

    Btt_DownLoadDir: TButton;

    IdFTP1: TIdFTP;

    Btt_DeleteDir: TButton;

    Label1: TLabel;

    lb_num: TLabel; //處理文件個數提示。

    PRocedure Btt_DownLoadDirClick(Sender: TObject);

    procedure Btt_DeleteDirClick(Sender: TObject);

  private

    { Private declarations }

  public

    { Public declarations }

  end;

var

  Form1: TForm1;

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.

 

 

運行環境 win2000 advanced server + delphi7.0 + iis6.0


上一篇:我寫的采用csv格式將數據轉換為excel的函數,帶有分欄功能

下一篇:獲取其他進程中ListBox和ComboBox的內容

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
學習交流
熱門圖片

新聞熱點

疑難解答

圖片精選

網友關注

主站蜘蛛池模板: 寿阳县| 涿州市| 金华市| 德保县| 榆社县| 洞口县| 黄大仙区| 张家口市| 遂溪县| 西昌市| 昌都县| 周至县| 宝鸡市| 吴忠市| 通海县| 湘潭县| 娄底市| 尖扎县| 施秉县| 分宜县| 大英县| 澎湖县| 武川县| 西宁市| 二连浩特市| 海晏县| 太原市| 平谷区| 亚东县| 阿拉尔市| 泰州市| 霍邱县| 辉南县| 望奎县| 鄂伦春自治旗| 惠安县| 乌什县| 峡江县| 阳江市| 巴南区| 米易县|