刚收到《程序员大本营2001》的光盘,发现一个现成的例子,贴上来。unit FileSearchUnit;(* TFileSearch by Nicola {MSX} Lugato [email protected] http://msx.rules.it A simple component that incapsulate FindFirst FindNext and FindClose procedures Usage: You can call OpenSearch to start a new search with specified arguments. Once opened call NextMatch to get results until it returns false. Than call CloseSearch. Alternatively you can call PerformeAction. With PerformeAction you specify the action to performe on each file that respond to your search in metod OnPerformeAction. In this case you can also use the IncludeSubdirectory to perform the action even on subdirs. if you like this component and use it, please tell me: [email protected] *)interfaceuses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;type TOnActionEvent = procedure (Sender: TObject; Path:String; FileName:String) of object; type TFileSearch = class(TComponent) private searching:boolean; action:TOnActionEvent; first:boolean; ffilter,fpath:string; fattr:integer; sr:TSearchRec; { Private declarations } protected { Protected declarations } public constructor Create(AOwner: TComponent); override; procedure OpenSearch(path:string;filter:string; attributes:integer); procedure CloseSearch; procedure PerformAction(path:string;filter:string; attributes:integer; IncludeSubdirectory:boolean); function NextMatch(var SearchRec:TSearchRec):boolean; published property OnPerformeAction: TOnActionEvent read Action write Action; end;procedure Register;implementationprocedure AdjustSlash(var s:string); begin if s[length(s)]<>'\' then s:=s+'\'; end;procedure TFileSearch.PerformAction(path:string;filter:string; attributes:integer; includesubdirectory:boolean); var x:TSearchRec; s:TstringList; i:integeR; fs:TFileSearch; begin OpenSearch(path, filter, attributes); while NextMatch(x) do if Assigned(action) then begin // Use this to reset file attributes //SetFileAttributes(pchar(fpath+x.Name),FILE_ATTRIBUTE_NORMAL); action(self, fpath, x.Name); end; CloseSearch;if IncludeSubdirectory then begin s:=TStringList.create; OpenSearch(path, '*.*', faDirectory); while NextMatch(x) do if ((x.Attr and fadirectory)<>0) and (x.Name<>'.') and (x.Name<>'..') then s.Add(x.Name); CloseSearch; AdjustSlash(path); fs:=TFileSearch.Create(self); fs.OnPerformeAction:=action; for i:=0 to s.Count-1 do fs.PerformAction(path+s[i],filter,attributes,true); fs.free; end; end;constructor TFileSearch.Create(AOwner: TComponent); begin inherited Create(AOwner); searching:=false; first:=false; end;function TFileSearch.NextMatch(var SearchRec:TSearchRec):boolean; begin if first then begin result:=(FindFirst(fPAth+ffilter,fAttr,sr)=0); first:=false; end else result:=(FindNext(sr)=0);SearchRec:=sr; end;procedure TFileSearch.OpenSearch(path:string;filter:string; attributes:integer); begin if searching then raise Exception.Create('TFileSearch: Already searching. Use CloseSearch to terminate previous search.'); adjustslash(path); ffilter:=filter; fpath:=path; fattr:=attributes; searching:=true; first:=true; end;procedure TFileSearch.CloseSearch; begin if not searching then raise Exception.Create('TFileSearch: Not searching. Use CloseSearch only after OpenSearch.'); FindClose(sr); searching:=false; first:=true; end;procedure Register; begin RegisterComponents('MSX', [TFileSearch]); end;end.
还是用stringlist比较好
http://msx.rules.it A simple component that incapsulate
FindFirst FindNext and FindClose procedures Usage: You can call OpenSearch to start a new search with specified
arguments. Once opened call NextMatch to get results until
it returns false. Than call CloseSearch. Alternatively you can call PerformeAction. With
PerformeAction you specify the action to performe on each
file that respond to your search in metod OnPerformeAction.
In this case you can also use the IncludeSubdirectory to
perform the action even on subdirs. if you like this component and use it, please
tell me: [email protected]
*)interfaceuses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;type TOnActionEvent = procedure (Sender: TObject; Path:String; FileName:String) of object;
type
TFileSearch = class(TComponent)
private
searching:boolean;
action:TOnActionEvent;
first:boolean;
ffilter,fpath:string;
fattr:integer;
sr:TSearchRec;
{ Private declarations }
protected
{ Protected declarations }
public
constructor Create(AOwner: TComponent); override;
procedure OpenSearch(path:string;filter:string; attributes:integer);
procedure CloseSearch;
procedure PerformAction(path:string;filter:string; attributes:integer; IncludeSubdirectory:boolean);
function NextMatch(var SearchRec:TSearchRec):boolean; published property OnPerformeAction: TOnActionEvent read Action write Action;
end;procedure Register;implementationprocedure AdjustSlash(var s:string);
begin
if s[length(s)]<>'\' then s:=s+'\';
end;procedure TFileSearch.PerformAction(path:string;filter:string; attributes:integer; includesubdirectory:boolean);
var x:TSearchRec;
s:TstringList;
i:integeR;
fs:TFileSearch;
begin
OpenSearch(path, filter, attributes);
while NextMatch(x) do
if Assigned(action) then
begin
// Use this to reset file attributes
//SetFileAttributes(pchar(fpath+x.Name),FILE_ATTRIBUTE_NORMAL);
action(self, fpath, x.Name);
end;
CloseSearch;if IncludeSubdirectory then
begin
s:=TStringList.create;
OpenSearch(path, '*.*', faDirectory);
while NextMatch(x) do
if ((x.Attr and fadirectory)<>0) and (x.Name<>'.') and (x.Name<>'..') then
s.Add(x.Name);
CloseSearch;
AdjustSlash(path);
fs:=TFileSearch.Create(self);
fs.OnPerformeAction:=action;
for i:=0 to s.Count-1 do
fs.PerformAction(path+s[i],filter,attributes,true);
fs.free;
end;
end;constructor TFileSearch.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
searching:=false;
first:=false;
end;function TFileSearch.NextMatch(var SearchRec:TSearchRec):boolean;
begin
if first then
begin
result:=(FindFirst(fPAth+ffilter,fAttr,sr)=0);
first:=false;
end
else
result:=(FindNext(sr)=0);SearchRec:=sr;
end;procedure TFileSearch.OpenSearch(path:string;filter:string; attributes:integer);
begin
if searching then
raise Exception.Create('TFileSearch: Already searching. Use CloseSearch to terminate previous search.');
adjustslash(path);
ffilter:=filter;
fpath:=path;
fattr:=attributes;
searching:=true;
first:=true;
end;procedure TFileSearch.CloseSearch;
begin
if not searching then
raise Exception.Create('TFileSearch: Not searching. Use CloseSearch only after OpenSearch.');
FindClose(sr);
searching:=false;
first:=true;
end;procedure Register;
begin
RegisterComponents('MSX', [TFileSearch]);
end;end.
存为名称和Unit说明一样的名称,然后再‘Component\Install Component'
选取该文件,OK后,出现一个对话框,选Inatall.