2008. január 10., csütörtök

How to delete multiple files from a directory


Problem/Question/Abstract:

How to delete multiple files from a directory

Answer:

procedure DeleteFiles(sMask, sPath: string);
var
  SearchRec: TSearchRec;
  Found: Integer;
begin
  sPath := IncludeTrailingPathDelimiter(sPath);
  Found := SysUtils.FindFirst(sPath + sMask, faAnyFile, SearchRec);
  try
    while (Found = 0) do
    begin
      if not (SearchRec.Attr and faDirectory > 0) then
        SysUtils.DeleteFile(sPath + SearchRec.Name);
      Found := SysUtils.FindNext(SearchRec);
    end;
  finally
    SysUtils.FindClose(SearchRec);
  end;
end;

Usage:

DeleteFiles('*.txt', 'c:\myfiles');

Nincsenek megjegyzések:

Megjegyzés küldése