网上有好多清空指定目录及子目录文件的函数,但没有可以指定扩展名的,自己写了一个

{$WARN SYMBOL_PLATFORM OFF}
function DeletePath(mDirName: string; Ext: String = '*'): Boolean;
var
  vSearchRec: TSearchRec;
  vPathName, tmpExt: string;
  K: Integer;
begin
  Result := true;

  tmpExt := Ext;
  if Pos('.', tmpExt) = 0 then
    tmpExt := '.' + tmpExt;

  vPathName := mDirName + '\*.*';
  K := FindFirst(vPathName, faAnyFile, vSearchRec);
  while K = 0 do
  begin
    if (vSearchRec.Attr and faDirectory > 0) and
      (Pos(vSearchRec.Name, '..') = 0) then
    begin
      FileSetAttr(mDirName + '\' + vSearchRec.Name, faDirectory);
      Result := DeletePath(mDirName + '\' + vSearchRec.Name, Ext);
    end
    else if Pos(vSearchRec.Name, '..') = 0 then
    begin
      FileSetAttr(mDirName + '\' + vSearchRec.Name, 0);
      if ((CompareText(tmpExt, ExtractFileExt(vSearchRec.Name)) = 0) or (CompareText(tmpExt, '.*') = 0)) then
        Result := DeleteFile(PChar(mDirName + '\' + vSearchRec.Name));
    end;
    if not Result then
      Break;
    K := FindNext(vSearchRec);
  end;
  FindClose(vSearchRec);
end;

Posted on 2007-01-10 06:01  Clark Zheng  阅读(2228)  评论(5编辑  收藏  举报