Programming C# C++ (7) Delphi (619) .NET (2) Database (72) Delphi IDE (90) Network (39) Printing (3) Strings (12) VCL (83) Windows with Delphi (280) Java (8) JavaScript (27) perl (9) php (4) VBScript (1) Visual Basic (1)
Exchange Links About this site Links to us 
|
Recursively delete a group of files and subdirectories
3 comments. Current rating: (2 votes). Leave comments and/ or rate it.
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.
 | |  | |
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);
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;
| |  | |  |
Comments:
|
|
|
|
add {$I-} directive before rmdir to ignore errors:
{$I-}
RmDir (Path + SearchRec.Name);
|
|
|
|
|
It was a great help. Thanks
|
|
|
|
|
thanks bro, this is brilliant code saved me a ton of time, THANK YOU SO MUCH!
|
|