DelphiFAQ Home Search:
General :: Programming :: Delphi
General Delphi related information.

Articles:

This list is sorted by recent document popularity (not total page views).
New documents will first appear at the bottom.
Recommended links on this topic:
Featured Article

Recursively delete a group of files and subdirectories

Use the following routine do delete all files matching a certain mask within a given directory.
If you set the third parameter to TRUE, subdirectires are scanned/ deleted also.

{**Summary ======================================================
     NAME    :   DeleteFiles
     PARAMS  :   const Path, Mask : string; recursive : boolean
 
     RETURNS :   -
     PURPOSE :   delete several files with joker.
                 Optional recursive = search in subdirectories.                
 
     EXAMPLE :   DeleteFiles ('c:\temp\', '*.txt', True);
     UPDATES :
 
     NOTES   :
     CODED   :   PT  15.10.96
   ===============================================================*}
 procedure DeleteFiles (const Path, Mask : string; recursive : boolean);
 var
   Result    : integer;
   SearchRec : TSearchRec;
 begin
   Result := FindFirst(Path + Mask, faAnyFile - faDirectory, SearchRec);
   while Result = 0 do
   begin
     if not DeleteFile (Path + SearchRec.name) then
     begin
       FileSetAttr (Path + SearchRec.name, 0); { reset all flags }
       DeleteFile (Path + SearchRec.name);
     end;
     Result := FindNext(SearchRec);
   end;
   FindClose(SearchRec);
 
   if not recursive then
     exit;
 
   Result := FindFirst(Path + '*.*', faDirectory, SearchRec);
   while Result = 0 do
   begin
     if (SearchRec.name <> '.') and (SearchRec.name <> '..') then
     begin
       FileSetAttr (Path + SearchRec.name, faDirectory);
       DeleteFiles (Path + SearchRec.name + '\', Mask, TRUE);
       RmDir (Path + SearchRec.name);
     end;
     Result := FindNext(SearchRec);
   end;
   FindClose(SearchRec);
 end;
 

Generated 0:01:39 on Dec 15, 2017