//获取指定目录下的文件列表,支持通配符。 procedure GetDirFileList(aTypeName, aDirName: String; var ReturnStringList: TStringList; isAndDirectory: Boolean);-------------//获取指定目录下的文件名称 procedure TDirectory.GetDirFileList(aTypeName, aDirName: String; var ReturnStringList: TStringList; isAndDirectory: Boolean); { 返回值为字符串列表 } var sr: TSearchRec; FileAttrs: Integer; begin ReturnStringList.Clear; aDirName := DecorateDirPath(aDirName); if isAndDirectory then FileAttrs := faDirectory else FileAttrs := faAnyFile; if FindFirst(aDirName + aTypeName, FileAttrs, sr) = 0 then begin repeat if (sr.Attr and FileAttrs) = sr.Attr then ReturnStringList.Add(sr.name); until FindNext(sr) <> 0; FindClose(sr); end; end; { GetDirFile }
和 CDSoftwareWj(95927) 兄 竞争一下 procedure SearchCurrentFiles(const Path,FileType:string; Strings: TStrings); var Search:TSearchRec; iCount:integer; FilePath:string; begin FilePath:=Path; if Path[Length(Path)]<>'\' then FilePath:=Path+'\'; Try iCount:=FindFirst(FilePath +'*.*',faAnyFile,Search); while iCount =0 do begin if (Search.name<>'.')and(Search.name<>'..') then begin //if ExtractFileExt(Search.Name)=trim(FileType) then Strings.Add(Search.Name); end; iCount:=FindNext(Search) end; finally FindClose(Search); end;end; procedure TForm1.Button1Click(Sender: TObject); var Path,FileType:string; begin Path:='F:\mp3\'; //FileType:='mp3' ; SearchCurrentFiles(Path,FileType,ListBox1.Items); end;
这是我写的一个控件的一部分代码,可以完成你想要的功能使用时要修改一下这里procedure TDirectory.GetDirFileList ^^^^^^^^^^例子: var tempStrList: TStringList; begin ..... //Create tempStrList ..... GetDirFileList('*.pas', 'c:\temp', tempStrList, False); // False - 文件 True - 目录 ..... // tempStrList中就是文件列表了 end;下面是 function TDirectory.DecorateDirPath(aDirPath: String): string; begin if aDirPath[Length(aDirPath)]<> '\' then Result := aDirPath+'\' else Result := aDirPath; end; 功能不用说了吧
procedure GetDirFileList(aTypeName, aDirName: String; var ReturnStringList:
TStringList; isAndDirectory: Boolean);-------------//获取指定目录下的文件名称
procedure TDirectory.GetDirFileList(aTypeName, aDirName: String; var
ReturnStringList: TStringList; isAndDirectory: Boolean);
{ 返回值为字符串列表 }
var
sr: TSearchRec;
FileAttrs: Integer;
begin
ReturnStringList.Clear;
aDirName := DecorateDirPath(aDirName);
if isAndDirectory then
FileAttrs := faDirectory
else
FileAttrs := faAnyFile;
if FindFirst(aDirName + aTypeName, FileAttrs, sr) = 0 then
begin
repeat
if (sr.Attr and FileAttrs) = sr.Attr then
ReturnStringList.Add(sr.name);
until FindNext(sr) <> 0;
FindClose(sr);
end;
end; { GetDirFile }
procedure SearchCurrentFiles(const Path,FileType:string; Strings: TStrings);
var
Search:TSearchRec;
iCount:integer;
FilePath:string;
begin
FilePath:=Path;
if Path[Length(Path)]<>'\' then
FilePath:=Path+'\'; Try
iCount:=FindFirst(FilePath +'*.*',faAnyFile,Search);
while iCount =0 do
begin
if (Search.name<>'.')and(Search.name<>'..') then
begin
//if ExtractFileExt(Search.Name)=trim(FileType) then
Strings.Add(Search.Name);
end;
iCount:=FindNext(Search)
end;
finally
FindClose(Search);
end;end;
procedure TForm1.Button1Click(Sender: TObject);
var
Path,FileType:string;
begin
Path:='F:\mp3\';
//FileType:='mp3' ;
SearchCurrentFiles(Path,FileType,ListBox1.Items);
end;
^^^^^^^^^^例子:
var
tempStrList: TStringList;
begin
.....
//Create tempStrList
.....
GetDirFileList('*.pas', 'c:\temp', tempStrList, False); // False - 文件 True - 目录
.....
// tempStrList中就是文件列表了
end;下面是
function TDirectory.DecorateDirPath(aDirPath: String): string;
begin
if aDirPath[Length(aDirPath)]<> '\' then
Result := aDirPath+'\'
else
Result := aDirPath;
end;
功能不用说了吧