參考一下: procedure TForm1.Button1Click(Sender: TObject); var ss:TSearchRec; filepath:string; begin filepath:='c:\'; listbox1.Items.Clear; if FindFirst(filepath+'\*.*',faanyfile,ss)=0 then begin if not ((ss.attr and fadirectory)=fadirectory) then listbox1.items.Add(ss.name); while findnext(ss)=0 do begin if not((ss.attr and fadirectory)=fadirectory) then listbox1.items.add(ss.Name); end; findclose(ss); end; end;
unit MainFrm;interfaceuses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, FileCtrl, Grids, Outline, DirOutln;type TMainForm = class(TForm) dcbDrives: TDriveComboBox; edtFileMask: TEdit; lblFileMask: TLabel; btnSearchForFiles: TButton; lbFiles: TListBox; dolDirectories: TDirectoryOutline; procedure btnSearchForFilesClick(Sender: TObject); procedure dcbDrivesChange(Sender: TObject); private FFileName: String; function GetDirectoryName(Dir: String): String; procedure FindFiles(APath: String); end;var MainForm: TMainForm;implementation{$R *.DFM}function TMainForm.GetDirectoryName(Dir: String): String; { This function formats the directory name so that it is a valid directory containing the back-slash (\) as the last character. } begin if Dir[Length(Dir)]<> '\' then Result := Dir+'\' else Result := Dir; end;procedure TMainForm.FindFiles(APath: String); { This is a procedure which is called recursively so that it finds the file with a specified mask through the current directory and its sub-directories. } var FSearchRec, DSearchRec: TSearchRec; FindResult: integer; function IsDirNotation(ADirName: String): Boolean; begin Result := (ADirName = '.') or (ADirName = '..'); end;begin APath := GetDirectoryName(APath); // Obtain a valid directory name { Find the first occurrence of the specified file name } FindResult := FindFirst(APath+FFileName,faAnyFile+faHidden+ faSysFile+faReadOnly,FSearchRec); try { Continue to search for the files according to the specified mask. If found, add the files and their paths to the listbox.} while FindResult = 0 do begin lbFiles.Items.Add(LowerCase(APath+FSearchRec.Name)); FindResult := FindNext(FSearchRec); end; { Now search the sub-directories of this current directory. Do this by using FindFirst to loop through each subdirectory, then call FindFiles (this function) again. This recursive process will continue until all sub-directories have been searched. } FindResult := FindFirst(APath+'*.*', faDirectory, DSearchRec); while FindResult = 0 do begin if ((DSearchRec.Attr and faDirectory) = faDirectory) and not IsDirNotation(DSearchRec.Name) then FindFiles(APath+DSearchRec.Name); // Recursion here FindResult := FindNext(DSearchRec); end; finally FindClose(FSearchRec); end; end;procedure TMainForm.btnSearchForFilesClick(Sender: TObject); { This method starts the searching process. It first changes the cursor to an hourglass since the process may take awhile. It then clears the listbox and calls the FindFiles() function which will be called recursively to search through sub-directories } begin Screen.Cursor := crHourGlass; try lbFiles.Items.Clear; FFileName := edtFileMask.Text; FindFiles(dolDirectories.Directory); finally Screen.Cursor := crDefault; end; end;procedure TMainForm.dcbDrivesChange(Sender: TObject); begin dolDirectories.Drive := dcbDrives.Drive; end;end.
procedure TForm1.Button1Click(Sender: TObject);
var ss:TSearchRec;
filepath:string;
begin
filepath:='c:\';
listbox1.Items.Clear;
if FindFirst(filepath+'\*.*',faanyfile,ss)=0 then
begin
if not ((ss.attr and fadirectory)=fadirectory) then
listbox1.items.Add(ss.name);
while findnext(ss)=0 do
begin
if not((ss.attr and fadirectory)=fadirectory) then
listbox1.items.add(ss.Name);
end;
findclose(ss);
end;
end;
Windows, Messages, SysUtils, Classes, Graphics, Controls,
Forms, Dialogs, StdCtrls, FileCtrl, Grids, Outline, DirOutln;type
TMainForm = class(TForm)
dcbDrives: TDriveComboBox;
edtFileMask: TEdit;
lblFileMask: TLabel;
btnSearchForFiles: TButton;
lbFiles: TListBox;
dolDirectories: TDirectoryOutline;
procedure btnSearchForFilesClick(Sender: TObject);
procedure dcbDrivesChange(Sender: TObject);
private
FFileName: String;
function GetDirectoryName(Dir: String): String;
procedure FindFiles(APath: String);
end;var
MainForm: TMainForm;implementation{$R *.DFM}function TMainForm.GetDirectoryName(Dir: String): String;
{ This function formats the directory name so that it is a valid
directory containing the back-slash (\) as the last character. }
begin
if Dir[Length(Dir)]<> '\' then
Result := Dir+'\'
else
Result := Dir;
end;procedure TMainForm.FindFiles(APath: String);
{ This is a procedure which is called recursively so that it finds the
file with a specified mask through the current directory and its
sub-directories. }
var
FSearchRec,
DSearchRec: TSearchRec;
FindResult: integer; function IsDirNotation(ADirName: String): Boolean;
begin
Result := (ADirName = '.') or (ADirName = '..');
end;begin
APath := GetDirectoryName(APath); // Obtain a valid directory name
{ Find the first occurrence of the specified file name }
FindResult := FindFirst(APath+FFileName,faAnyFile+faHidden+
faSysFile+faReadOnly,FSearchRec);
try
{ Continue to search for the files according to the specified
mask. If found, add the files and their paths to the listbox.}
while FindResult = 0 do
begin
lbFiles.Items.Add(LowerCase(APath+FSearchRec.Name));
FindResult := FindNext(FSearchRec);
end; { Now search the sub-directories of this current directory. Do this
by using FindFirst to loop through each subdirectory, then call
FindFiles (this function) again. This recursive process will
continue until all sub-directories have been searched. }
FindResult := FindFirst(APath+'*.*', faDirectory, DSearchRec); while FindResult = 0 do
begin
if ((DSearchRec.Attr and faDirectory) = faDirectory) and not
IsDirNotation(DSearchRec.Name) then
FindFiles(APath+DSearchRec.Name); // Recursion here
FindResult := FindNext(DSearchRec);
end;
finally
FindClose(FSearchRec);
end;
end;procedure TMainForm.btnSearchForFilesClick(Sender: TObject);
{ This method starts the searching process. It first changes the cursor
to an hourglass since the process may take awhile. It then clears the
listbox and calls the FindFiles() function which will be called
recursively to search through sub-directories }
begin
Screen.Cursor := crHourGlass;
try
lbFiles.Items.Clear;
FFileName := edtFileMask.Text;
FindFiles(dolDirectories.Directory);
finally
Screen.Cursor := crDefault;
end;
end;procedure TMainForm.dcbDrivesChange(Sender: TObject);
begin
dolDirectories.Drive := dcbDrives.Drive;
end;end.