Function SearchFileS(SPath,Filter:String;InChildDir : Boolean ; var StrLS : TStringList ) : Boolean; var i: Integer; SearchRec: TSearchRec; FileName : String; begin Result := True; if copy(Spath,Length(Spath),1)<>'\' then SPath := SPath+'\'; try i:=FindFirst(SPath+Filter,faAnyFile,SearchRec); except Result := False; exit; end; while i=0 do begin FileName := SearchRec.name; if Fileexists(SPath+FileName) then StrLS.Add(SPath+FileName) else if (FileName<>'.') and (FileName<>'..') then begin if InChildDir then SearchFileS(SPath+FileName,Filter,InChildDir,StrLS) else StrLS.Add(SPath+FileName); end else if FileName='..' then StrLS.Add(SPath); i := FindNext(SearchRec); end; try i:=FindFirst(SPath+'*.*',faDirectory,SearchRec); except Result := False; exit; end; while i=0 do begin FileName := SearchRec.name; if Fileexists(SPath+FileName) then else if (FileName<>'.') and (FileName<>'..') then begin if InChildDir then SearchFileS(SPath+FileName,Filter,InChildDir,StrLS) else StrLS.Add(SPath+FileName); end; // else if FileName='..' then StrLS.Add(SPath); i := FindNext(SearchRec); end;
end;使用如下:var FileList : TStringList;... SearchFileS('D:\','AAA.gdb',True,FileList); //保存第一个文件.. //然后删除 for i:=0 to FileList.Count-1 do DeleteFile(FileList.Strings[i]);
var
i: Integer;
SearchRec: TSearchRec;
FileName : String;
begin
Result := True;
if copy(Spath,Length(Spath),1)<>'\' then SPath := SPath+'\';
try
i:=FindFirst(SPath+Filter,faAnyFile,SearchRec);
except
Result := False;
exit;
end;
while i=0 do begin
FileName := SearchRec.name;
if Fileexists(SPath+FileName) then StrLS.Add(SPath+FileName)
else if (FileName<>'.') and (FileName<>'..') then
begin
if InChildDir then SearchFileS(SPath+FileName,Filter,InChildDir,StrLS)
else StrLS.Add(SPath+FileName);
end
else if FileName='..' then StrLS.Add(SPath);
i := FindNext(SearchRec);
end;
try
i:=FindFirst(SPath+'*.*',faDirectory,SearchRec);
except
Result := False;
exit;
end;
while i=0 do begin
FileName := SearchRec.name;
if Fileexists(SPath+FileName) then
else if (FileName<>'.') and (FileName<>'..') then
begin
if InChildDir then SearchFileS(SPath+FileName,Filter,InChildDir,StrLS)
else StrLS.Add(SPath+FileName);
end;
// else if FileName='..' then StrLS.Add(SPath);
i := FindNext(SearchRec);
end;
end;使用如下:var
FileList : TStringList;...
SearchFileS('D:\','AAA.gdb',True,FileList);
//保存第一个文件..
//然后删除
for i:=0 to FileList.Count-1 do
DeleteFile(FileList.Strings[i]);