Jump to content

Delete cache...


Eugeniusz Rink
 Share

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...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...