只要楼主结合起来用就可以了。
以下是完整代码
function TForm1.AddDirSuffix(Dir: string): string;
begin
Result := Trim(Dir);
if Result = '' then Exit;
if Result[Length(Result)] <> '/' then Result := Result + '/';
end;
function TForm1.Deltree(Dir: string): Boolean;
var
sr: TSearchRec;
fr: Integer;
begin
if not DirectoryExists(Dir) then
begin
Result := True;
Exit;
end;
fr := FindFirst(AddDirSuffix(Dir) + '*.*', faAnyFile, sr);
try
while fr = 0 do
begin
if (sr.Name <> '.') and (sr.Name <> '..') then
begin
if sr.Attr and faDirectory = faDirectory then
Result := Deltree(AddDirSuffix(Dir) + sr.Name)
else
Result := DeleteFile(AddDirSuffix(Dir) + sr.Name);
if not Result then
Exit;
end;
fr := FindNext(sr);
end;
finally
FindClose(sr);
end;
Result := RemoveDir(Dir);
end;
procedure TForm1.Button1Click(Sender: TObject);
var
F: TSearchRec;
i: integer;
begin
Memo1.Clear;
FindAllDir('C:/Windows/', Memo1.Lines);
for i := Memo1.Lines.Count - 1 downto 0 do begin
Deltree(Memo1.Lines);
Memo1.Lines.Delete(i);
end;
end;
function TForm1.FindAllDir(Path: string; AList: TStrings): Integer;
var
F: TSearchRec;
Ret: Integer;
begin
Result := 0;
Ret := FindFirst(Path + '$*$', faAnyFile, F);
while Ret = 0 do begin
if ((F.Attr and faDirectory) > 0) then begin
if (F.Name <> '.') and (F.Name <> '..') then
AList.Add(Path + F.Name);
end;
Ret := FindNext(F);
end;
FindClose(F);
end;