Visit our Sponsor   Visit our Sponsor
delphi3000.com - the free delphi knowledge platform
delphi3000.com - the free delphi knowledge platform
497 Users Online NOW
Have a look at your member-status

connecting people's knowledge


  - Recent ArticlesRSS feed for Recent Articles on delphi3000.com
  - List of All Articles
  - Top Viewed Articles
  - Articles (+Attachem.)
  - Articles Of Interest
  - Categories
  - Top Uploader
  - Search
  - Index

  - My Home
  - Submit an Article
  - My Articles
  - My Personal Data
  - My Bookmarks
  - Activities
  - Login/Logout

  - Sign Up
  - Why Sign Up
  - Newsletter

  - Press
  - Advertise

  - Contact
  - Feedback





Community
Borland
ClubeDelphi
Dr. Bob
UK-BUG
Delphi Meetings
Planeta Delphi



Loremo - the 1.5 liter car coming in 2009




Startblatt.de






Share this article with friendsShare this article with friends
Rate this articleRate this article - to keep the quality of delphi3000.com !
Comment this article or read through previous comments (0)


Delete Files from FileMask older than X daysFormat this article printer-friendly!Bookmark function is only available for registered users!
PurgeFiles()
Product:
Delphi all versions
Category:
Files Operation
Skill Level:
Scoring:
Last Update:
11/30/2005
Search Keys:
delphi delphi3000 article borland vcl code-snippet files delete purge
Times Scored:
2
Visits:
2796
Uploader: Mike Heydon
Company: EOH
Reference: mheydon@pgbison.co.za
 
Question/Problem/Abstract:
This function was prompted by the new MS SQL Express 2005 Database which does not include a SQL Agent that can set up Database Maintenance Plans. This means that it does not have the functionality to delete backup files older than X number of days automatically.

I have written function PurgeFiles() that can perfom this task.

function PurgeFiles(const AFileMask : string; AOlderThanDays : word; AFailOnError : boolean = false) : integer;

Delete files (AFileMask) from a directory that are older than AOlderThanDays 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

Examples

// No Error Check
iDel : integer;
iDel :=  PurgeFiles('c:\temp\*.txt',7);   // Delete all txt files older than 7 days

// With Error check
try
  iDel := PurgeFiles('c:\temp\*.bak,20,true);
except
  // handle your error here
end
Answer:




// =================================================================
// 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
// =================================================================

uses Windows,SysUtils;

function PurgeFiles(const AFileMask : string; AOlderThanDays : word;
                    AFailOnError : boolean = false) : integer;
var rDirInfo : TSearchRec;
    iResult,iError : integer;
    dtFileDate,dtNow : TDateTime;
    sFilePath,sErrMess : string;
begin
  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 := 'PurgFiles() Failed on file' + #13#10 +
                      sFilePath + rDirInfo.Name + #13#10#13#10 +
                      SysErrorMessage(GetLastError);
          raise Exception.Create(sErrMess);
        end;

        inc(iResult);
      end;
    end;

    iError := FindNext(rDirInfo);
    if iError <> 0 then FindClose(rDirInfo);  // Release FindFirt Allocs
  end;

  Result := iResult;
end;






Please rate this article!
Skill level:
BeginnerExpert

Useful:
No!Very!

Overall rating:
PoorExcellent



Comments to this article
Write a new comment













 
Sign up to consume product discounts for Bronze memberships !

read more


  Visit our Sponsor

 

  Community Ad of
D. Souchard
 
   














 







     
  Copyright © 2000 - 2007 delphi3000.com - All rights reserved. Terms of use. || Privacy
delphi3000.com is a service by bluestep.com IT-Services GmbH (Vienna)