全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:8542
推到 Plurk!
推到 Facebook!

複製整個 Directory 及刪除整個 Directory 兩個函式!

 
Dalman
一般會員


發表:27
回覆:22
積分:24
註冊:2002-08-21

發送簡訊給我
#1 引用回覆 回覆 發表時間:2002-10-09 14:44:53 IP:211.21.xxx.xxx 未訂閱
‧因專案上需要做整個目錄操作緣故,因此撰寫及測試過這個兩個函式。 ‧若各位大大用過 Win32 APIs SHFileOperation() 函式來複製整個目錄會 出現目錄檔案莫名地無法複製情況時,也請使用這兩個函式代替使用。    
//----------------------------------------------------------------------------    uses SysUtils;    //【函式作用】複製整個目錄至指定路徑內。
//【輸入引數】
//  vSrcPath        來源目錄路徑。
//  vDestPath       目的目錄路徑。
//  [vOverwrite]    選擇性引數,遇到相同檔名檔案存在時是否要覆寫,{true→覆寫|false→不覆寫}。@=true。
//【函式傳回】Boolean,{true→執行成功|false→執行失敗}。
//【作者版本】Dalman, 2002/10/09, 1.0.0。    function _CopyDir(const vSrcPath, vDestPath: AnsiString; vOverwrite: Boolean=true): Boolean;
var
  r: TSearchRec;
  s, d: AnsiString;
  ns, nd: AnsiString;
begin
  try
    try
      result := false;
      s := IncludeTrailingPathDelimiter(vSrcPath);
      d := IncludeTrailingPathDelimiter(vDestPath);
      if not ForceDirectories(d) then Exit;                                   //若目的目錄無法建立時,則...
      //---- 開始遞回搜尋複製目錄及檔案群 ------------------------------------
      if FindFirst(s   '*.*', faAnyFile, r) = 0 then begin
        repeat
          if (r.Name <> '.') and (r.Name <> '..') then begin
            ns := s   r.Name;                                                 //新來源目錄路徑或檔案名稱。
            nd := d   r.Name;                                                 //新目的目錄路徑或檔案名稱。
            if (r.Attr and faDirectory) = faDirectory then begin              //若為目錄時,則...
              if not ForceDirectories(nd) then Exit;                          //嚐試建立目的目錄。
              CopyDir(ns, nd);                                                //再找下一層子目錄複製。
            end else begin
              if FileExists(nd) and (not vOverwrite) then Continue;           //若目的檔案存在且又不覆寫時,則...
              if not CopyFile(PChar(ns), PChar(nd), false) then Exit;
            end;
          end;
        until FindNext(r) <> 0;
        result := true;
      end;
    except
      on e: Exception do result := false;
    end;
  finally
    FindClose(r);
  end;
end;
//----------------------------------------------------------------------------
//【函式作用】刪除整個目錄。
//【輸入引數】
//  vDirPath        來源目錄路徑。
//  [vForced]       選擇性引數,{true→徹底刪除整個目錄|false→若遇唯讀檔案時則略過刪除}。@=true。
//【函式傳回】Boolean,{true→執行成功|false→執行失敗}。
//【函式說明】
//  一、請小心使用此函式,目錄一經刪除則無法救回!
//  二、此函式無法刪除下列目錄或檔案:
//    ‧已經有使用者網路連線使用的共用目錄。(尚未有連線的共用目錄則可刪除)
//    ‧屬性設定為「faSysFile(系統檔案)」的目錄或檔案。
//【作者版本】Dalman, 2002/10/09, 1.0.0。    function _DeleteDir(const vDirPath: AnsiString; vForced: Boolean=true): Boolean;
var
  r: TSearchRec;
  d, nd: AnsiString;
begin
  try
    result := true;
    d := IncludeTrailingPathDelimiter(vDirPath);
    if DirectoryExists(d) then begin                                          //若指定目錄存在時,則...
      //---- 開始遞回搜尋刪除目錄及檔案群 ------------------------------------
      if FindFirst(d   '*.*', faAnyFile, r) = 0 then begin
        repeat
          if (r.Name <> '.') and (r.Name <> '..') then begin
            nd := d   r.Name;                                                 //新目的目錄路徑或檔案名稱。
            if vForced then FileSetAttr(nd, r.Attr and (not faReadOnly));     //若強迫刪除時,則先將檔案唯讀屬性取消。
            if (r.Attr and faDirectory) = faDirectory then                    //若為目錄時,則...
              DeleteDir(nd, vForced)
            else begin
              DeleteFile(PChar(nd));
            end;
          end;
        until FindNext(r) <> 0;
      end;
      FindClose(r);
      if vForced then FileSetAttr(d, FileGetAttr(d) and (not faReadOnly));    //若強迫刪除時,則先將目錄唯讀屬性取消。
      result := RemoveDir(d);                                                 //試著刪除目錄本身並傳回結果。
    end;
  except
    on e: Exception do begin result := false; FindClose(r); end;
  end;
end;    
【使用範例】 _CopyDir('C:\Program Files', 'C:\Temp'); _DeleteDir('C:\Temp', false);
kkccgg
初階會員


發表:50
回覆:54
積分:30
註冊:2002-07-28

發送簡訊給我
#2 引用回覆 回覆 發表時間:2003-08-25 21:42:03 IP:203.204.xxx.xxx 未訂閱
請問: 我用了該函數,不過出現下列錯誤 Undeclared identifier: IncludeTrailingPathDelimiter DirectoryExists DeleteDir uses如下: uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; win2000 Delphi5 thanks
hagar
版主


發表:143
回覆:4056
積分:4445
註冊:2002-04-14

發送簡訊給我
#3 引用回覆 回覆 發表時間:2003-08-26 06:43:28 IP:202.39.xxx.xxx 未訂閱
1.IncludeTrailingPathDelimiter Delph 5 沒有這個 function, 您可以 IncludeTrailingBackSlash 替代    2.DirectoryExists 要 uses FileCtrl;    3.DeleteDir ??? --- 歡迎光臨 ><>@ 發表人 -
pcboy
版主


發表:177
回覆:1838
積分:1463
註冊:2004-01-13

發送簡訊給我
#4 引用回覆 回覆 發表時間:2004-09-01 16:09:29 IP:210.69.xxx.xxx 未訂閱
在 Delphi 7 下 Compile 產生下面錯誤 [Warning] Unit1.pas(8): Unit 'FileCtrl' is specific to a platform [Error] Unit1.pas(57): Undeclared identifier: 'CopyDir' [Warning] Unit1.pas(100): Symbol 'FileSetAttr' is specific to a platform [Warning] Unit1.pas(100): Symbol 'faReadOnly' is specific to a platform [Error] Unit1.pas(102): Undeclared identifier: 'DeleteDir' [Warning] Unit1.pas(110): Symbol 'FileSetAttr' is specific to a platform [Warning] Unit1.pas(110): Symbol 'FileGetAttr' is specific to a platform [Warning] Unit1.pas(110): Symbol 'faReadOnly' is specific to a platform [Fatal Error] Project1.dpr(5): Could not compile used unit 'Unit1.pas'
------
能力不足,求助於人;有能力時,幫幫別人;如果您滿意答覆,請適時結案!

子曰:問有三種,不懂則問,雖懂有疑則問,雖懂而想知更多則問!
zombit
初階會員


發表:63
回覆:39
積分:30
註冊:2004-05-11

發送簡訊給我
#5 引用回覆 回覆 發表時間:2005-06-12 04:54:40 IP:61.62.xxx.xxx 未訂閱
修正 Compiler 出現的錯誤, 請將函式名 _CopyDir 改成 CopyDir, _DeleteDir 改成 DeleteDir, 函式動作正常.
bruce0211
版主


發表:157
回覆:668
積分:279
註冊:2002-06-13

發送簡訊給我
#6 引用回覆 回覆 發表時間:2005-06-12 08:48:23 IP:59.120.xxx.xxx 未訂閱
或可參考 Delphi K.Top電子報 第034期 http://delphi.ktop.com.tw/topic.php?topic_id=41363 有 delphi & bcb 版 _DelTree() 刪除整個目錄(含子目錄) _XCopy() 複製整個目錄(含子目錄) _Move() 搬移整個目錄(含子目錄) 發表人 - bruce0211 於 2005/06/12 08:49:20
系統時間:2024-04-19 19:19:15
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!