可以使用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;
下面提供了两种方法,虽然方法二看起来比较复杂,不过推荐使用,应为你可以根据自己得需求灵活控制 unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;type TForm1 = class(TForm) Button1: TButton; Memo1: TMemo; ListBox1: TListBox; Button2: TButton; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); private { Private declarations } public procedure Findds(AStrings: TStrings; APath, Sourfile: string); end;var Form1: TForm1;implementation{$R *.dfm}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; 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;调试时findfirst返回没有找到文件 但我MP3目录下明明是有MP3文件的,请帮忙看一下问题出在了那里
使用findfirst以及findnext查找文件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;
begin
Found := FindFirst("c:\dir\1.mp3", Attr, SearchRec);
while Found = 0 do
begin
ProcessSearchRec(SearchRec);
Found := FindNext(SearchRec);
end;
FindClose(SearchRec);
end;
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
ListBox1: TListBox;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
procedure Findds(AStrings: TStrings; APath, Sourfile: string);
end;var
Form1: TForm1;implementation{$R *.dfm}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;
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;调试时findfirst返回没有找到文件
但我MP3目录下明明是有MP3文件的,请帮忙看一下问题出在了那里
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;