03 Juni 2009

Hapus File dalam direktori secara Rekursif

{
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;