Jump to content

Delete cache...


Eugeniusz Rink

Recommended Posts

  • 1 year later...
  • 6 months later...
1 hour ago, artem_niko said:

Anybody have ideas?

Hi, I don't know what you need to do to make unigui clear cache files, but you can create your own function that clears files older than a few days when uniGui Server starts or using TThreatTimer

here's a few examples: https://stackoverflow.com/questions/54321253/delphi-delete-files-in-a-directory-older-than-x-days-and-or-having-a-special-fi

http://www.delphigroups.info/2/35/521671.html

search results: https://www.google.com/search?q=delphi+clear+older+files&oq=delphi+clear+older+files&aqs=chrome..69i57j0i22i30j0i390.6735j0j7&sourceid=chrome&ie=UTF-8

Link to comment
Share on other sites

On 11/19/2022 at 4:28 PM, eduardosuruagy said:

I was wondering how you guys managed to delete cache folders in a certain number of days?

I use this in my desktop application to only delete files from a temporary directory.

// =================================================================
// Delete files (mask) from a directory that are older than X days
// Returns number of files deleted, but will raise an exception if
// unable to delete a file (eg. Read Only attribute) if
// AFailOnError is true.
//
// NOTE : AOlderThanDays = 0 will delete ALL files matching mask
// =================================================================

function sysDeleteOlderFiles(const AFileMask : AnsiString; AOlderThanDays : word; AFailOnError : boolean = false) : AnsiString;
var
  rDirInfo : TSearchRec;
  iResult,iError : integer;
  dtFileDate,dtNow : TDateTime;
  sFilePath,sErrMess : string;
  label ENDAll;
begin
  sFunctionName := 'sysDeleteOlderFiles';
  sErrMess := '';
  iResult := 0;
  dtNow := Date;
  sFilePath := ExtractFilePath(AFileMask);
  iError := FindFirst(AFileMask,faAnyFile,rDirInfo);

  // Itterate thru files found with mask
  while iError = 0 do begin
    // Eclude Directories
    if (rDirInfo.Name <> '.')
    and (rDirInfo.Name <> '..')
    and (rDirInfo.Attr
          and faDirectory  <> faDirectory) then begin
      dtFileDate := FileDateToDateTime(rDirInfo.Time);

      // Does the file meet deletion days criteria ?
      if trunc(dtNow - dtFileDate) + 1  >= AOlderThanDays then begin
        // Delete the file - raise exception if fail and AFailOnError set
        if not DeleteFile (sFilePath + rDirInfo.Name)
        and AFailOnError then begin
           sErrMess := 'Failed on file: ' + sFilePath + rDirInfo.Name + '\n ' + SysErrorMessage(GetLastError);
           raise Exception.Create(sErrMess);
        end;

        inc(iResult);
      end;
    end;

    iError := FindNext(rDirInfo);
  end;

  EndAll : begin
      FindClose(rDirInfo);
      if sErrMess <> '' then
           Result := sErrMess
      else Result := '';//IntToStr (iResult);
  end;
end;
 

 

Link to comment
Share on other sites

  • 1 month later...
  • 1 year later...

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
×
×
  • Create New...