首页  编辑  

删除指定目录

Tags: /超级猛料/Friends.网友专栏/zswang/函数大全/   Date Created:

(*//

标题:删除指定目录

说明:支持删除有只读、隐藏属性的文件或目录

设计:Zswang

日期:2002-01-25

支持:wjhu111@21cn.com

//*)

///////Begin Source

function DeletePath(mDirName: string): Boolean; { 返回删除指定目录是否成功 }

var

 vSearchRec: TSearchRec;

 vPathName: string;

 K: Integer;

begin

 Result := True;

 vPathName := mDirName + '\*.*';

 K := FindFirst(vPathName, faAnyFile, vSearchRec);

 while K = 0 do begin

   if (vSearchRec.Attr and faDirectory > 0) and

     (Pos(vSearchRec.Name, '..') = 0) then begin

     FileSetAttr(mDirName + '\' + vSearchRec.Name, faDirectory);

     Result := DeletePath(mDirName + '\' + vSearchRec.Name);

   end else if Pos(vSearchRec.Name, '..') = 0 then begin

     FileSetAttr(mDirName + '\' + vSearchRec.Name, 0);

     Result := DeleteFile(PChar(mDirName + '\' + vSearchRec.Name));

   end;

   if not Result then Break;

   K := FindNext(vSearchRec);

 end;

 FindClose(vSearchRec);

 Result := RemoveDir(mDirName);

end; { DeletePath }

///////End Source

///////Begin Demo

procedure TForm1.Button1Click(Sender: TObject);

begin

 DeletePath(Edit1.Text);

end;

///////End Demo