今天结贴,如何列出一个文件夹中的所有文件(100分)

L

lah998

Unregistered / Unconfirmed
GUEST, unregistred user!
一:如何列出一个文件夹中的所有文件和文件夹中的所有文件夹,并形成一个清单。
清单要求包括每个文件的完整路径、文件名、(文件大小及文件属性)这两个可以不要。
注:文件夹中有子文件夹,要求子文件夹中的文件也要列出来。我要完整的源码能在D6
中通过,请不要提醒我搜以前的贴子!

二:请问下面这段代码里的“&apos”是什么意思?为什么在我的机上D6报告这个有错!
if (FindFirst(mainpath+'*.*', faDirectory, SearchRec)=0) then
注:小妹初学还望大哥加点中文注解,不胜感谢!
 
if (FindFirst(mainpath+'*.*', faDirectory, SearchRec)=0) then

那是HTML中的引号

参考http://www.delphibbs.com/delphibbs/dispq.asp?lid=1313570
自己改一下:
procedure TForm1.Button2Click(Sender: TObject);
var
SR:TSearchRec;
filter:TStrings;
s:string;
begin
filter:=TStringList.create;
filter.add('BMP');
filter.add('SCR');
filter.add('EXE');
if FindFirst('c:/windows/system/*.*', $3f, sr)=0 then
begin
while FindNext(sr)=0 do
begin
s:=trim(UpperCase(extractFileExt(sr.Name)));
if length(s)=0 then continue;
if s[1]='.' then s:=copy(s,2,length(s)-1);
if filter.IndexOf(s)>=0 then showmessage(sr.Name);
end;
FindClose(sr);
end;
filter.free;
end;
 
问题1:
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
function isvalidfile(searchrec:tsearchrec):boolean;
function searchFile(filepath:string;filelist:tstringlist):boolean;
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

function tform1.isvalidfile(searchrec:tsearchrec):boolean;
begin
if (searchrec.Attr=16) or (searchrec.Attr=8) then
result:=false
else result:=true;
end;

function tform1.searchFile(filepath:string;filelist:tstringlist):boolean;
var searchrec:tsearchrec;
searchstr:string;
begin
if filelist=nil then
begin
result:=false;
exit;
end
else
begin
filelist.Clear;
end;
result:=false;
if copy(filepath,length(filepath),1)<>'/' then
searchstr:=filepath+'/*.*';
if (findfirst(searchstr,faanyfile,searchrec)=0) then
begin
result:=true;
if isvalidfile(searchrec) then
filelist.Add(searchrec.Name);
while (findnext(searchrec)=0) do
begin
if isvalidfile(searchrec) then
filelist.Add(searchrec.Name);
end;
end;
findclose(searchrec);
end;

procedure TForm1.Button1Click(Sender: TObject);
var filelist:tstringlist;
i:integer;
begin
filelist:=tstringlist.Create;
if searchFile(trim(edit1.Text),filelist) then
begin
memo1.Clear;

for i:=0 to filelist.Count-1 do
begin
memo1.Lines.Add(filelist.Strings);
end;
end;
end;

end.

如果文件夹中还有文件,可以循环之。
 
如用delhpi的功能,只能循环找
 
是否搜索子目录?
 
to:jsxjd
你的只是指定文件夹下的指定扩展名的文件,不能列出文件夹下的文件夹!

to:晴
你的代码虽能找出指定文件夹下的所有文件的文件名,非文件夹名!
而且只有文件名,不是全路径!
to SupermanTm
当然要而且要列出来
 
多人接受答案了。
 
顶部