procedure TForm1.Findds(AStrings: TStrings; APath, Sourfile: string);//这是方法二得实现函数
var
FSearchRec : TSearchRec;
FindResult : integer;
TmpList:TStringList;
i : integer;
function IsDirNot(A : string) : boolean;
begin
Result := (a = '.') or (a = '..');
end;
begin
try
TmpList:=TStringList.Create;
TmpList.Clear;
FindResult := FindFirst(Apath+Sourfile,faDirectory,FSearchRec);
while FindResult = 0 do
begin
if ((FSearchRec.Attr and fadirectory) = faDirectory) then
begin
if not IsDirNot(FSearchRec.Name) then begin
tmplist.Add(apath+FSearchRec.Name);
Findds(AStrings, APath + FSearchRec.Name + '\',Sourfile);
end;
end else tmplist.Add(apath+FSearchRec.Name);
FindResult := FindNext(FSearchRec);
end;
for i := 0 to TmpList.Count -1 do
AStrings.Add(TmpList.Strings[i]);
TmpList.Free;
finally
FindClose(FSearchRec);
end;
end;
我写了下面这段测试代码
procedure TForm1.Button1Click(Sender: TObject);
var
Mp3Path :string;
sr :Tsearchrec;
begin
mp3path:=extractfilepath(application.ExeName)+'mp3\*.mp3';
if findfirst(mp3path,faanyfile,sr) = 0 then
begin
repeat
if sr.Name = '*.mp3' then
memo1.Lines.Add(sr.Name);
until FindNext(sr) <> 0;
FindClose(sr);
end;
end;
procedure TForm1.Button1Click(Sender: TObject);//方法1
var
s:String;
begin
s:='d:\temp\*.*';
SendMessage(ListBox1.Handle,LB_DIR,DDL_ARCHIVE or DDL_HIDDEN or DDL_SYSTEM or DDL_READWRITE,Integer(s));
end;
procedure TForm1.Button2Click(Sender: TObject);//方法二
begin
Findds(Memo1.Lines,'d:\a\','*.mp3');
end;
procedure TForm1.Findds(AStrings: TStrings; APath, Sourfile: string);//这是方法二得实现函数
var
FSearchRec : TSearchRec;
FindResult : integer;
TmpList:TStringList;
i : integer;
function IsDirNot(A : string) : boolean;
begin
Result := (a = '.') or (a = '..');
end;
begin
try
TmpList:=TStringList.Create;
TmpList.Clear;
FindResult := FindFirst(Apath+Sourfile,faDirectory,FSearchRec);
while FindResult = 0 do
begin
if ((FSearchRec.Attr and fadirectory) = faDirectory) then
begin
if not IsDirNot(FSearchRec.Name) then begin
tmplist.Add(apath+FSearchRec.Name);
Findds(AStrings, APath + FSearchRec.Name + '\',Sourfile);
end;
end else tmplist.Add(apath+FSearchRec.Name);
FindResult := FindNext(FSearchRec);
end;
for i := 0 to TmpList.Count -1 do
AStrings.Add(TmpList.Strings[i]);
TmpList.Free;
finally
FindClose(FSearchRec);
end;
end;
可以使用FindFirst和FindNext函数查找文件,如:
begin
Found := FindFirst("c:\dir\1.mp3", Attr, SearchRec);
while Found = 0 do
begin
ProcessSearchRec(SearchRec);
Found := FindNext(SearchRec);
end;
FindClose(SearchRec);
end;