读书人

帮小弟我看看这个查找文件的函数怎么修

发布时间: 2012-03-03 15:33:04 作者: rapoo

帮我看看这个查找文件的函数如何修改?
如何在一个指定目录以及子目录下查找某些文件,并把结果存放在fileflist中,其中Findname可能取‘c:\*.dll’,FLIST可能是‘C:\WINDOWS\111.DLL,C:\PROGRAMFILE\ZZ.DLL’。

procedure searching(filename:string;fileflist:Tstring);
var
sr: TSearchRec;
Attr: Integer;
begin
Attr := faAnyFile;
if FindFirst(filename,Attr,sr)=0 then
begin
repeat
fileflist.Add(sr.Name);
until(FindNext(sr)<>0);
end;
FindClose(sr);
end;

我这个程序只能查根目录下的文件,无法查子目录下的文件。
应该是用递归法吧,请问如何修改?

[解决办法]
看下我回的旧帖
http://topic.csdn.net/t/20060711/22/4874357.html
[解决办法]

Delphi(Pascal) code
procedure searching(filename:string;fileflist:Tstrings);var  sr: TSearchRec;begin  if FindFirst(filename+'\*.*',faAnyFile,sr)=0 then  begin    repeat      if pos('.dll',lowercase(sr.Name))>0 then        fileflist.Add(filename+'\'+sr.Name)      else if ((sr.Attr and faDirectory)<>0) and (sr.Name<>'.') and (sr.Name<>'..') then        searching(filename+'\'+sr.Name,fileflist);    until(FindNext(sr)<>0);  end;  FindClose(sr);end;searching('C:',Memo1.Lines) ;
[解决办法]
// 应用程序路径
function AppPath: string;
begin
Result := ExtractFilePath(Application.ExeName);
end;

// 返回从左边第一为开始切取 CutLeng长度的字符串
function RightStr(psInput:String; CutLeng:Integer):String;
begin
Result:=Copy(psInput,Length(psInput)-CutLeng+1,CutLeng)
end;

// ================================================================
// 遍历某个文件夹及子文件夹下某种文件,
// 使用说明
// G _GetFileList(ListBox1.Items, 'c:\', '*.doc');
// _GetFileList(MyTStringList, 'c:\', '*.exe');
// ================================================================
procedure _GetFileList(AStrings: TStrings; ASourFile,
FileName: string);
var sour_path,sour_file: string;
TmpList:TStringList;
FileRec, subFileRec:TSearchrec;
i: Integer;
begin
if rightStr(trim(ASourFile), 1) <> '\' then
sour_path :=trim(ASourFile) + '\'
else
sour_path :=trim(ASourFile);
sour_file:= FileName;

if not DirectoryExists(sour_path) then
begin
AStrings.Clear;
exit;
end;

TmpList:=TStringList.Create;
TmpList.Clear;

if FindFirst(sour_path+'*.*',faAnyfile,FileRec) = 0 then
repeat
if ((FileRec.Attr and faDirectory) <> 0) then
begin
if ((FileRec.Name<> '.') and (FileRec.Name <> '..')) then
_GetFileList(AStrings, sour_path+ FileRec.Name + '\', sour_file);
end
else
if FindFirst(sour_path + FileName,faAnyfile,subFileRec) = 0 then
repeat
if ((subFileRec.Attr and faDirectory) = 0) then
TmpList.Add(sour_path+subFileRec.Name);
until FindNext(subFileRec)<>0;

until FindNext(FileRec)<>0;

SysUtils.FindClose(FileRec);
for i := 0 to TmpList.Count -1 do
AStrings.Add(TmpList.Strings[i]);

TmpList.Free;
end;

读书人网 >.NET

热点推荐