Я не нашел встроенную функцию Delphi для этой конкретной проблемы.
Эта функция работала для меня:
function TUtilities.DeleteFilesOlderThanXDays(
Path: string;
DaysOld: integer = 0; // 0 => Delete every file, ignoring the file age
FileMask: string = '*.*'): integer;
var
iFindResult : integer;
SearchRecord : tSearchRec;
iFilesDeleted: integer;
begin
iFilesDeleted := 0;
iFindResult := FindFirst(TPath.Combine(Path, FileMask), faAnyFile, SearchRecord);
if iFindResult = 0 then begin
while iFindResult = 0 do begin
if ((SearchRecord.Attr and faDirectory) = 0) then begin
if (FileDateToDateTime(SearchRecord.Time) < Now - DaysOld) or (DaysOld = 0) then begin
DeleteFile(TPath.Combine(Path, SearchRecord.Name));
iFilesDeleted := iFilesDeleted + 1;
end;
end;
iFindResult := FindNext(SearchRecord);
end;
FindClose(SearchRecord);
end;
Result := iFilesDeleted;
end;