比较急!如何获取某目录下所有MP3文件?

fscyber 2006-08-17 04:53:24
有那位高手可以指点一下如何获取目录下的所有MP3文件啊?
...全文
251 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
wudi_1982 2006-08-17
  • 打赏
  • 举报
回复
使用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;
fscyber 2006-08-17
  • 打赏
  • 举报
回复
我写了下面这段测试代码
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文件的,请帮忙看一下问题出在了那里
postren 2006-08-17
  • 打赏
  • 举报
回复
看Windows SDK的帮助
wudi_1982 2006-08-17
  • 打赏
  • 举报
回复
下面提供了两种方法,虽然方法二看起来比较复杂,不过推荐使用,应为你可以根据自己得需求灵活控制
unit Unit1;

interface

uses
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.
Elysium 2006-08-17
  • 打赏
  • 举报
回复
可以使用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;
fscyber 2006-08-17
  • 打赏
  • 举报
回复
Win32FindFirst是什么冬冬?我在delphi的help里面找不到它啊
ly_liuyang 2006-08-17
  • 打赏
  • 举报
回复
遍历搜索
Win32FindFirst/Next

16,748

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 语言基础/算法/系统设计
社区管理员
  • 语言基础/算法/系统设计社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧