程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> Delphi >> 在Delphi中實現對目錄拷貝、刪除和搬移的操作

在Delphi中實現對目錄拷貝、刪除和搬移的操作

編輯:Delphi

筆者在工作中遇到了需要對目錄進行拷貝、刪除和搬移的需求,Delphi本身提供了一些目錄操作函數,但只是針對空目錄而言,對目錄下帶有子目錄的情況,更是無能為力。

利用Win32 API函數和結構,以及遞歸的程序設計思想,筆者實現了對任意目錄進行拷貝、刪除和搬移的功能(分別相當於DOS中的XCopy、DelTree和Move命令)。以下分別給出了實現代碼: 

1、拷貝目錄 

為了能拷貝目錄下帶有子目錄的情況,先定義一個輔助的拷貝函數,它是遞歸執行的,直到把目錄下的所有文件和子目錄都拷貝完。

1.1拷貝目錄的遞歸輔助函數:DoCopyDir 

function DoCopyDir(sDirName:String;
sToDirName:String):Boolean;
var
  hFindFile:Cardinal;
  t,tfile:String;
  sCurDir:String[255];
  FindFileData:WIN32_FIND_DATA;
begin
  //先保存當前目錄
  sCurDir:=GetCurrentDir;
  ChDir(sDirName);
  hFindFile:=FindFirstFile('*.*',FindFileData);
  if hFindFile< >INVALID_HANDLE_VALUE then
  begin
     if not DirectoryExists(sToDirName) then
      ForceDirectories(sToDirName);
     repeat
        tfile:=FindFileData.cFileName;
        if (tfile='.') or (tfile='..') then
         Continue;
        if FindFileData.dwFileAttributes=
        FILE_ATTRIBUTE_DIRECTORY then
        begin
          t:=sToDirName+'\'+tfile;
          if not DirectoryExists(t) then
            ForceDirectories(t);
          if sDirName[Length(sDirName)]< >'\' then
            DoCopyDir(sDirName+'\'+tfile,t)
          else
            DoCopyDir(sDirName+tfile,sToDirName+tfile);
        end
        else
        begin
          t:=sToDirName+'\'+tFile;
          CopyFile(PChar(tfile),PChar(t),True);
        end;
     until FindNextFile(hFindFile,FindFileData)=false;
     FindClose(hFindFile);
  end
  else
  begin
     ChDir(sCurDir);
     result:=false;
     exit;
  end;
  //回到原來的目錄下
  ChDir(sCurDir);
  result:=true;
end;

1.2 拷貝目錄的函數:CopyDir 

function CopyDir(sDirName:String;
sToDirName:string):Boolean;
begin
    if Length(sDirName)< =0 then
exit;
//拷貝...
Result:=DoCopyDir(sDirName,sToDirName);
end;

2、刪除目錄

刪除目錄與拷貝目錄很類似,但為了能刪除位於根目錄下的一個空目錄,需要在輔助函數中設置一個標志變量,即:如果刪除的是空目錄,則置bEmptyDir為True,這一句已經用深色框表示了。

2.1刪除目錄的遞歸輔助函數:DoRemoveDir

function DoRemoveDir(sDirName:String):Boolean;
var
hFindFile:Cardinal;
tfile:String;
sCurDir:String;
bEmptyDir:Boolean;
FindFileData:WIN32_FIND_DATA;
begin
//如果刪除的是空目錄,則置bEmptyDir為True
//初始時,bEmptyDir為True
bEmptyDir:=True;
//先保存當前目錄
sCurDir:=GetCurrentDir;
SetLength(sCurDir,Length(sCurDir));
ChDir(sDirName);
hFindFile:=FindFirstFile('*.*',FindFileData);
if hFindFile< >INVALID_HANDLE_VALUE then
  begin
     repeat
        tfile:=FindFileData.cFileName;
        if (tfile='.') or (tfile='..') then
        begin
         bEmptyDir:=bEmptyDir and True;
         Continue;
        end;
        //不是空目錄,置bEmptyDir為False
        bEmptyDir:=False;
        if FindFileData.dwFileAttributes=
        FILE_ATTRIBUTE_DIRECTORY then
        begin
          if sDirName[Length(sDirName)]< >'\' then
            DoRemoveDir(sDirName+'\'+tfile)
          else
            DoRemoveDir(sDirName+tfile);
          if not RemoveDirectory(PChar(tfile)) then
            result:=false
          else
            result:=true;
        end
        else
        begin
          if not DeleteFile(PChar(tfile)) then
            result:=false
          else
            result:=true;
        end;
     until FindNextFile(hFindFile,FindFileData)=false;
     FindClose(hFindFile);
  end
  else
  begin
     ChDir(sCurDir);
     result:=false;
     exit;
  end;
  //如果是空目錄,則刪除該空目錄
  if bEmptyDir then
  begin
     //返回上一級目錄
     ChDir('..');
     //刪除空目錄
     RemoveDirectory(PChar(sDirName));
  end;
  //回到原來的目錄下
  ChDir(sCurDir);
  result:=true;
end;

2.2刪除目錄的函數:DeleteDir 

function DeleteDir(sDirName:String):Boolean;
begin
    if Length(sDirName)< =0 then
exit;
//刪除...
Result:=DoRemoveDir(sDirName) and RemoveDir(sDirName);
end;

3、移動目錄

有了拷貝目錄和刪除目錄的函數,移動目錄就變得很簡單,只需順序調用前兩個函數即可:

function MoveDir(sDirName:String;
sToDirName:string):Boolean;
begin
if CopyDir(sDirName,sToDirName) then
if RemoveDir(sDirName) then
result:=True
else
result:=false;
end;

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved