Delete files > 7 days old in a selected directory

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.

LVL 1
chart11610Asked:
Who is Participating?
 
shaneholmesConnect With a Mentor Commented:
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
 
shaneholmesCommented:
Opps, sorry about the delete part :

OTTOMH something like this:


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

shaneholmes
0
 
ezraaCommented:
Check out: TEasyFileSearch here: http://www.torry.net/search.htm
0
 
Russell LibbySoftware Engineer, Advisory Commented:

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
 
shaneholmesCommented:
Was there something wrong with my code - that did not work?

ShaneHolmes
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.