?
Solved

Delete files > 7 days old in a selected directory

Posted on 2005-04-18
5
Medium Priority
?
934 Views
Last Modified: 2010-04-05
I have a directory with *.PDF documents in it.  Many of these get system generated.
I know the name of directory where with files are stored.  May even be on a network share.
Want simple loop that can look at all of the files (no sub directories) in the specified directory, and remove all files more that X days old including 100% of the files in the directory.

0
Comment
Question by:chart11610
  • 3
5 Comments
 
LVL 11

Accepted Solution

by:
shaneholmes earned 375 total points
ID: 13807507
Hi,

Here is a simple method.

Drop a TFileListbox on your form

Set its properties at design time or runtime (as i did in the FormActivate)


procedure TForm1.FormActivate(Sender: TObject);
begin
 FileListBox1.Visible:= True;
 FileListBox1.Mask:= '*.pdf';
 FileListBox1.Directory:= 'C:\Program Files\Borland\Delphi7\Projects\Preso\Drawings\PDFs';
end;



Now i placed a routine in a Button's onClick, but you can do with the code as you like

procedure TForm1.Button1Click(Sender: TObject);
var
 I: Integer;
 H: THandle;
 R: Integer;
 D: TDateTime;
begin
 for I:= 0 to  FileListBox1.Items.Count - 1 do
 begin
 //open the file and get a file handle
  H:= FileOpen(FileListBox1.Items[I] , fmOpenReadWrite or fmShareExclusive);
 //get DOS date-and-time stamp of the file given by Handle
  R:= FileGetDate(H);
 //convert the returned DOS date-time value to a TDateTime
  D:= FileDateToDateTime(R);
 //do your date logic/compare here
  if D > Now then ShowMessage('OK');
 end;
end;

end.


0
 
LVL 11

Expert Comment

by:shaneholmes
ID: 13807550
Opps, sorry about the delete part :

OTTOMH something like this:


if D <= Date - 7 then DeleteFile(FileListBox1.Items[I]);

shaneholmes
0
 
LVL 9

Expert Comment

by:ezraa
ID: 13807610
Check out: TEasyFileSearch here: http://www.torry.net/search.htm
0
 
LVL 26

Expert Comment

by:Russell Libby
ID: 13808381

Another alternative

////////////////////////////////////////////////////////////////////////////////
//
//   DeleteFilesByDate
//
//      Path        -  Path where files are located
//      FileSpec    -  File specification. If blank, then "*" will be used (all files)
//      DateCheck   -  DateTime to use to determine if files should be deleted.
//
//   returns the number of files deleted.
//
////////////////////////////////////////////////////////////////////////////////
function DeleteFilesByDate(Path, FileSpec: String; DateCheck: TDateTime): Integer;
var  szPath:        String;
     szFileSpec:    String;
     srFind:        TSearchRec;
     dwFind:        Integer;
     dtDate:        TDateTime;
begin

  // Set default result
  result:=0;

  // Path set
  szPath:=ExcludeTrailingBackslash(Path);

  // FileSpec handling
  if (FileSpec = EmptyStr) then
     szFileSpec:='*'
  else
     szFileSpec:=FileSpec;

  // Start find enumeration
  dwFind:=FindFirst(Format('%s\%s', [szPath, szFileSpec]), faAnyFile, srFind);

  // Check enumeration
  if (dwFind = 0) then
  begin
     // Enumerate all files
     while (dwFind = 0) do
     begin
        // Check for file
        if ((srFind.Attr and faDirectory) = 0) then
        begin
           // Check file date
           if (FileDateToDateTime(srFind.Time) < DateCheck) then
           begin
              // Make sure file is not set to read only (which will fail a deletion)
              FileSetAttr(Format('%s\%s', [szPath, srFind.Name]), faArchive);
              // Attempt delete, if successful then increment result
              if DeleteFile(Format('%s\%s', [szPath, srFind.Name])) then Inc(result);
           end;
        end;
        // Find next
        dwFind:=FindNext(srFind);
     end;
     // Close enumeration
     FindClose(srFind);
  end;

end;


Regards,
Russell
0
 
LVL 11

Expert Comment

by:shaneholmes
ID: 13809069
Was there something wrong with my code - that did not work?

ShaneHolmes
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question