function ClearDir(const Dir: string): Boolean;
var
FileInfo: TSearchRec;
DosCode: Integer;
begin
Result := DirectoryExists(Dir);
if not Result then Exit;
DosCode := FindFirst(GetPath(Dir) + '*.*', faAnyFile, FileInfo);
try
while DosCode = 0 do begin
if (FileInfo.Name[1] <> '.') and (FileInfo.Attr <> faVolumeID) then
begin
if (FileInfo.Attr and faDirectory = faDirectory) then
Result := DelDir(GetPath(Dir) + FileInfo.Name)
else if (FileInfo.Attr and faVolumeID <> faVolumeID) then begin
if (FileInfo.Attr and SysUtils.faReadOnly = SysUtils.faReadOnly) then
FileSetAttr(GetPath(Dir) + FileInfo.Name, faArchive);
Result := DeleteFile(GetPath(Dir) + FileInfo.Name) and Result;
end;
end;
DosCode := SysUtils.FindNext(FileInfo);
end;
finally
FindClose(FileInfo);
end;
end;
这是一个清除目录的例子,可以参考一下