function JudgeDir(Attr:integer):boolean;
{判断是否是目录}
var
i:integer;
begin
i:=Attr; if i>=32 then i:=i-32; //排除文档文件
if i>=16
then Result:=true
else Result:=false; //返回是否是目录
end;
function DelTree(Dir:string):integer;
{删除整个目录,含出错处理,返回值为出错的文件数目}
var
Sr:TSearchRec; Err,ErrorFile,i:integer;
CurFilePath,TempFilePath:string;
begin
ErrorFile:=0; //初始化错误文件数
CurFilePath:=Dir; TempFilePath:=CurFilePath; //初始化
Err:=FindFirst(Dir+'\*.*',$37,Sr); //查找第一个文件
while (Err = 0) do
begin
if Sr.Name[1]<>'.' //判断特殊目录"."和".."
then begin
if JudgeDir(Sr.Attr)
then begin //处理目录情况
TempFilePath:=CurFilePath; //保存当前目录
CurFilePath:=CurFilePath+'\'+Sr.Name;
i:=DelTree(CurFilePath); //递归调用
if i<>0 then ErrorFile:=ErrorFile+i-1;
ChDir('..'); //返回上一级目录
if not RemoveDir(CurFilePath)
then ErrorFile:=ErrorFile+1; //删除目录
CurFilePath:=TempFilePath; //恢复当前目录
end
else begin //处理文件情况
if not DeleteFile(CurFilePath+'\'+Sr.Name)
then ErrorFile:=ErrorFile+1;
end;
end;
Err:=FindNext(Sr); //查找下一个文件或目录
end;
ChDir('..'); //返回总目录
if not RemoveDir(Dir) then ErrorFile:=ErrorFile+1;
//处理无法删除总目录
Result:=ErrorFile; //返回出错的文件数目
end;