findfirst只能尋找某目錄下的文件﹐怎樣可以做到尋找它子目錄下的文件呢﹖(50分)

  • 主题发起人 主题发起人 shuquanhe
  • 开始时间 开始时间
S

shuquanhe

Unregistered / Unconfirmed
GUEST, unregistred user!
findfirst只能尋找某目錄下的文件﹐怎樣可以做到尋找它子目錄下的文件呢﹖
 
自己写查找,用递归的方法。
或者在LMD 控件包中有这样的控件
 
给你一个例子: 递归的方法搜寻
unit Unit1;

interface

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

type
TForm1 = class(TForm)
Button2: TButton;
Memo1: TMemo;
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
procedure SearchFile(const Directy ,FileName:string);
implementation

{$R *.DFM}

procedure SearchFile(const Directy ,FileName:string);
var
FindRec:TWIN32FindData;
FindHandle :THandle;
FileAttr :DWORD ;
FileSearched :String ;
begin
FindHandle := FindFirstFile (pchar(Directy + FileName ),FindRec );
if FindHandle <> INVALID_HANDLE_VALUE
then begin
repeat
FileAttr := FindRec .dwFileAttributes;
FileSearched := string(FindRec .cFileName) ;
begin
Form1 .Memo1 .Lines .Add (Directy +FileSearched );
end;
if ( FileAttr = FILE_ATTRIBUTE_DIRECTORY) and ( FileSearched <> '.')
and ( FileSearched <> '..')
then begin
SearchFile (Directy+FileSearched +'/' ,FileName );
end;
until FindNextFile (FindHandle ,FindRec ) = false ;
end ;
windows. FindClose(FindHandle );
end;


procedure TForm1.Button2Click(Sender: TObject);
begin
SearchFile ('c:/','*.*');
end;

end.
 
procedure tform1.findfile(directory:string);
//directory备注:字串末尾无'/'
var
s:tsearchrec;
i:integer;
begin
i:=findfirst(directory+'/*.*',faAnyFile,s);
while i=0 do
begin
if s.name[1]<>'.' then
begin
if (s.attr and fadirectory)=fadirectory then
findfile(directory+'/'+s.name)
else
memo1.lines.add(directory+'/'+s.name);
end;
i:=findnext(s);
end;
end;

procedure tform1.button1click(sender:tobject);
begin
findfile('c:');//用法
end;
我没测试,大概就这些了
还有什么问提QQ:12584606 验证:大富翁
Email:china_kuchong@163.com
 
接受答案了.
 
后退
顶部