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