Monday, 1 July 2013

Delete files with a wildcard

Here is how to delete files with a wildcard. It requires SysUtls in the uses clause.

procedure DeleteFiles(FilePath, Wildcard: string);
var cSearchRec: TSearchRec;
    iFind     : integer;
    sPath     : string;
begin
    sPath := IncludeTrailingPathDelimiter(FilePath);
    iFind := FindFirst(sPath + Wildcard, faAnyFile, cSearchRec);
    while iFind = 0 do
    begin
        DeleteFile(sPath + cSearchRec.Name);
        iFind := FindNext(cSearchRec);
    end;
    FindClose(cSearchRec);

end;

In the while loop you can add additional conditions, like only files that have a specific prefix e.g.

if Pos(fPrefix, Copy(cSearchRec.Name, 0, Length(fPrefix))) > 0 then

No comments:

Post a Comment