以前的两个函数,你看看吧... --------------------------- function GetFilenames(FilePath: string):TStrings; //遍历目录下的文件名 var FileRec :TSearchrec; begin if DirectoryExists(FilePath) then begin Result:=TStringList.Create; if FindFirst(FilePath+'*.*',faAnyfile,FileRec) = 0 then repeat if (FileRec.Attr and faDirectory) = 0 then Result.Add(ExtractFileName(FilePath + FileRec.Name)); until FindNext(FileRec) <> 0; FindClose(FileRec); end; end;function GetFilenamesEx(FilePath: string):TStrings; //遍历目录及子目录下的文件名 var FileRec :TSearchrec; begin if DirectoryExists(FilePath) then begin Result:=TStringList.Create; if FindFirst(FilePath+'*.*',faAnyfile,FileRec) = 0 then repeat if (FileRec.Attr and faDirectory) <> 0 then begin if (FileRec.Name<> '.') and (FileRec.Name <> '..') then Result.AddStrings(GetFilenames(FilePath + FileRec.Name + '\')); end else Result.Add(ExtractFileName(FilePath + FileRec.Name)); until FindNext(FileRec) <> 0; FindClose(FileRec); end; end; procedure TForm1.Button1Click(Sender: TObject); //测试部分 begin ListBox1.Items := GetFilenames('c:\1\'); ListBox2.Items := GetFilenamesEx('c:\1\'); end;
考虑不周,现加了文件名后缀过滤... -_-# --------------------------------------function GetFilenames(FilePath,ExtMask: String):TStrings; //遍历目录 var FileRec :TSearchrec; begin if DirectoryExists(FilePath) then begin Result:=TStringList.Create; if FindFirst(FilePath+ExtMask,faAnyfile,FileRec) = 0 then repeat if (FileRec.Attr and faDirectory) = 0 then Result.Add(ExtractFileName(FilePath + FileRec.Name)); until FindNext(FileRec) <> 0; FindClose(FileRec); end; end;function GetFilenamesEx(FilePath,ExtMask :String):TStrings; //遍历目录及子目录 var FileRec :TSearchrec; begin if DirectoryExists(FilePath) then begin Result:=TStringList.Create; if FindFirst(FilePath+ExtMask,faAnyfile,FileRec) = 0 then repeat if (FileRec.Attr and faDirectory) <> 0 then begin if (FileRec.Name<> '.') and (FileRec.Name <> '..') then Result.AddStrings(GetFilenames(FilePath + FileRec.Name + '\',ExtMask)); end else Result.Add(ExtractFileName(FilePath + FileRec.Name)); until FindNext(FileRec) <> 0; FindClose(FileRec); end; end; procedure TForm1.Button1Click(Sender: TObject); begin ListBox1.Items := GetFilenames('c:\1\','*.txt'); ListBox2.Items := GetFilenamesEx('c:\1\','*.txt'); end;
能不能实现过滤呢,比如只需要txt文件,而不需要别的格式
---------------------------
function GetFilenames(FilePath: string):TStrings; //遍历目录下的文件名
var
FileRec :TSearchrec;
begin
if DirectoryExists(FilePath) then
begin
Result:=TStringList.Create;
if FindFirst(FilePath+'*.*',faAnyfile,FileRec) = 0 then
repeat
if (FileRec.Attr and faDirectory) = 0 then
Result.Add(ExtractFileName(FilePath + FileRec.Name));
until FindNext(FileRec) <> 0;
FindClose(FileRec);
end;
end;function GetFilenamesEx(FilePath: string):TStrings; //遍历目录及子目录下的文件名
var
FileRec :TSearchrec;
begin
if DirectoryExists(FilePath) then
begin
Result:=TStringList.Create;
if FindFirst(FilePath+'*.*',faAnyfile,FileRec) = 0 then
repeat
if (FileRec.Attr and faDirectory) <> 0 then
begin
if (FileRec.Name<> '.') and (FileRec.Name <> '..') then
Result.AddStrings(GetFilenames(FilePath + FileRec.Name + '\'));
end
else Result.Add(ExtractFileName(FilePath + FileRec.Name));
until FindNext(FileRec) <> 0;
FindClose(FileRec);
end;
end;
procedure TForm1.Button1Click(Sender: TObject); //测试部分
begin
ListBox1.Items := GetFilenames('c:\1\');
ListBox2.Items := GetFilenamesEx('c:\1\');
end;
--------------------------------------function GetFilenames(FilePath,ExtMask: String):TStrings; //遍历目录
var
FileRec :TSearchrec;
begin
if DirectoryExists(FilePath) then
begin
Result:=TStringList.Create;
if FindFirst(FilePath+ExtMask,faAnyfile,FileRec) = 0 then
repeat
if (FileRec.Attr and faDirectory) = 0 then
Result.Add(ExtractFileName(FilePath + FileRec.Name));
until FindNext(FileRec) <> 0;
FindClose(FileRec);
end;
end;function GetFilenamesEx(FilePath,ExtMask :String):TStrings; //遍历目录及子目录
var
FileRec :TSearchrec;
begin
if DirectoryExists(FilePath) then
begin
Result:=TStringList.Create;
if FindFirst(FilePath+ExtMask,faAnyfile,FileRec) = 0 then
repeat
if (FileRec.Attr and faDirectory) <> 0 then
begin
if (FileRec.Name<> '.') and (FileRec.Name <> '..') then
Result.AddStrings(GetFilenames(FilePath + FileRec.Name + '\',ExtMask));
end
else Result.Add(ExtractFileName(FilePath + FileRec.Name));
until FindNext(FileRec) <> 0;
FindClose(FileRec);
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
ListBox1.Items := GetFilenames('c:\1\','*.txt');
ListBox2.Items := GetFilenamesEx('c:\1\','*.txt');
end;