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 }
procedure TForm1.DoDelTree(TheDir : String); Var Search : TSearchRec; rec : word; Begin If TheDir[Length(TheDir)] <> '\' Then TheDir := TheDir + '\'; rec := FindFirst(TheDir + '*.*', faAnyFile, Search); While rec = 0 Do Begin If Search.Name[1] <> '.' Then Begin // Is this a directory? If (Search.Attr And faDirectory) = faDirectory Then Begin // If so, lets call DelTree again using this new // directory as the TheDir parameter. DoDelTree(TheDir + Search.Name); // Not that all of the files are gone from this directoy, // we can remove the directory. RmDir(TheDir + Search.Name); End Else Begin // We found a file. // Now lets reset its attributes so we don't have any problems // deleting them. FileSetAttr(TheDir + Search.Name, 0); DeleteFile(TheDir + Search.Name); Application.ProcessMessages; End; End; rec := FindNext(Search); End; FindClose(Search); End;procedure TForm1.Deltree(DirToKill : String; KillChoosenDir : Boolean); begin {$I-} DoDelTree(DirToKill); // If we want to delete the choosen directory. If KillChoosenDir Then RmDir(DirToKill); //modified if IOResult <> 0 then ShowMessage('Could not delete ' + DirToKill); {$I+} end;
procedure Tform_server.DeleteDirectoy(SDirName: string); var sr : TSearchRec; s : string; begin if FindFirst(Sdirname+'\*.*', faAnyFile, sr) = 0 then repeat s := sr.Name; if (s = '.') or (s = '..') then continue; if (sr.Attr =faDirectory) then begin DeleteDirectoy(Sdirname+'\'+s); RemoveDirectory(PChar(Sdirname + '\')); end else deletefile(pchar(sDirname+'\'+s)); until FindNext(sr)<> 0; RemoveDirectory(PChar(Sdirname + '\')); end;
Windows Shell编程!
再用RmDir(目录名)删除目录;
搞定!
还有一种方法是添加一个ShellAPI单元,然后里面有一个SHFile......(具体什么忘记了)函数可以直接删除非空文件夹,不过参数有点多
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 }
Var
Search : TSearchRec;
rec : word;
Begin
If TheDir[Length(TheDir)] <> '\' Then TheDir := TheDir + '\';
rec := FindFirst(TheDir + '*.*', faAnyFile, Search);
While rec = 0 Do
Begin
If Search.Name[1] <> '.' Then
Begin
// Is this a directory?
If (Search.Attr And faDirectory) = faDirectory Then
Begin
// If so, lets call DelTree again using this new
// directory as the TheDir parameter.
DoDelTree(TheDir + Search.Name);
// Not that all of the files are gone from this directoy,
// we can remove the directory.
RmDir(TheDir + Search.Name);
End
Else
Begin
// We found a file.
// Now lets reset its attributes so we don't have any problems
// deleting them.
FileSetAttr(TheDir + Search.Name, 0);
DeleteFile(TheDir + Search.Name);
Application.ProcessMessages;
End;
End;
rec := FindNext(Search);
End;
FindClose(Search);
End;procedure TForm1.Deltree(DirToKill : String; KillChoosenDir : Boolean);
begin
{$I-}
DoDelTree(DirToKill);
// If we want to delete the choosen directory.
If KillChoosenDir Then
RmDir(DirToKill);
//modified
if IOResult <> 0 then
ShowMessage('Could not delete ' + DirToKill);
{$I+}
end;
var
sr : TSearchRec;
s : string;
begin
if FindFirst(Sdirname+'\*.*', faAnyFile, sr) = 0 then
repeat
s := sr.Name;
if (s = '.') or (s = '..') then continue;
if (sr.Attr =faDirectory) then
begin
DeleteDirectoy(Sdirname+'\'+s);
RemoveDirectory(PChar(Sdirname + '\'));
end
else deletefile(pchar(sDirname+'\'+s));
until FindNext(sr)<> 0;
RemoveDirectory(PChar(Sdirname + '\'));
end;
dir.wFunc :=FO_DELETE ;
dir.pFrom :=pchar(c:\test\*.*') ;
SHFileOperation(dir) ;