遍历目录,cAkk的,cAkk别气哦! 一个函数,结果用参数foundresult返回: function SearchFile(mainpath:string;filename:string; var foundresult:TStrings):Boolean; var i:integer; Found:Boolean; subdir1:TStrings; searchRec:TsearchRec; begin if right(mainpath,1)<>'\' then mainpath:=mainpath+'\'; found:=false; if Trim(filename)<>'' then begin subdir1:=TStringList.Create; if (FindFirst(mainpath+'*.*', faDirectory,SearchRec)=0) then begin if IsValidDir(SearchRec) then subdir1.Add(SearchRec.Name); while (FindNext(SearchRec) = 0) do begin if IsValidDir(SearchRec) then subdir1.Add(SearchRec.Name); end; end; FindClose(SearchRec); //查找当前目录。 if (FindFirst(mainpath+'*.*', faAnyFile-faDirectory, SearchRec)=0) then begin foundresult.Add(mainpath+SearchRec.Name); while (FindNext(SearchRec) = 0) do begin foundresult.Add(mainpath+SearchRec.Name); end; end; FindClose(SearchRec); for i:=0 to subdir1.Count-1 do found:=Searchfile(mainpath+subdir1.Strings[i]+ '\',Filename,foundresult)or found; subdir1.Free; end; result:=found; end;
至于mp3的信息看你要什么信息了,这里与歌曲名称,和Artist Mp3有个Tag,看看这段程序 type TMP3Tag = record idTAG : array [0..2] of Char; idSongName :array [0..29] of Char; idArtist :array [0..29] of Char; end; function GetNameFromTag(Mp3File:TFileName):string; var Mp3Stream : TFileStream; ID3 : TMP3Tag; begin ZeroMemory(@ID3,SizeOf(ID3)); Mp3Stream := TFileStream.Create(Mp3File,fmOpenRead); Mp3Stream.Seek(-128, soFromEnd); Mp3Stream.Read(ID3, SizeOf(ID3)); Mp3Stream.Free; if ID3.idTAG ='TAG' then begin if Trim(ID3.idArtist)='' then Result := Trim(ID3.idSongName) else Result := Trim(ID3.idArtist) + '-' + Trim(ID3.idSongName); if UpperCase(ExtractFileExt(Result)) <> '.MP3' then Result := Result + '.mp3'; end else Result := Mp3File; end;
不好意思啊,落下一个函数 function IsValidDir(SearchRec:TSearchRec):Boolean; begin if (SearchRec.Attr=16) and (SearchRec.Name<>'.') and (SearchRec.Name<>'..') then Result:=True else Result:=False; end;
一个函数,结果用参数foundresult返回:
function SearchFile(mainpath:string;filename:string; var foundresult:TStrings):Boolean;
var
i:integer;
Found:Boolean;
subdir1:TStrings;
searchRec:TsearchRec;
begin
if right(mainpath,1)<>'\' then mainpath:=mainpath+'\';
found:=false;
if Trim(filename)<>'' then
begin
subdir1:=TStringList.Create;
if (FindFirst(mainpath+'*.*', faDirectory,SearchRec)=0) then
begin
if IsValidDir(SearchRec) then subdir1.Add(SearchRec.Name);
while (FindNext(SearchRec) = 0) do
begin
if IsValidDir(SearchRec) then subdir1.Add(SearchRec.Name);
end;
end;
FindClose(SearchRec);
//查找当前目录。
if (FindFirst(mainpath+'*.*', faAnyFile-faDirectory, SearchRec)=0) then
begin
foundresult.Add(mainpath+SearchRec.Name);
while (FindNext(SearchRec) = 0) do
begin
foundresult.Add(mainpath+SearchRec.Name);
end;
end;
FindClose(SearchRec);
for i:=0 to subdir1.Count-1 do
found:=Searchfile(mainpath+subdir1.Strings[i]+
'\',Filename,foundresult)or found;
subdir1.Free;
end;
result:=found;
end;
Mp3有个Tag,看看这段程序
type
TMP3Tag = record
idTAG : array [0..2] of Char;
idSongName :array [0..29] of Char;
idArtist :array [0..29] of Char;
end;
function GetNameFromTag(Mp3File:TFileName):string;
var
Mp3Stream : TFileStream;
ID3 : TMP3Tag;
begin
ZeroMemory(@ID3,SizeOf(ID3));
Mp3Stream := TFileStream.Create(Mp3File,fmOpenRead);
Mp3Stream.Seek(-128, soFromEnd);
Mp3Stream.Read(ID3, SizeOf(ID3));
Mp3Stream.Free;
if ID3.idTAG ='TAG' then
begin
if Trim(ID3.idArtist)='' then Result := Trim(ID3.idSongName)
else Result := Trim(ID3.idArtist) + '-' + Trim(ID3.idSongName);
if UpperCase(ExtractFileExt(Result)) <> '.MP3' then Result := Result + '.mp3';
end
else Result := Mp3File;
end;
function IsValidDir(SearchRec:TSearchRec):Boolean;
begin
if (SearchRec.Attr=16) and
(SearchRec.Name<>'.') and
(SearchRec.Name<>'..') then
Result:=True
else
Result:=False;
end;