您的位置:首页 > 编程语言 > Delphi

DELPHI删除指定目录下指定扩展名的文件

2007-01-10 06:01 495 查看

网上有好多清空指定目录及子目录文件的函数,但没有可以指定扩展名的,自己写了一个
{$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;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: