关于windows ApI(50分)

  • 主题发起人 主题发起人 xiewenrui
  • 开始时间 开始时间
X

xiewenrui

Unregistered / Unconfirmed
GUEST, unregistred user!
各位大虾:<br>&nbsp; &nbsp;你们好,请问在win2000下在delphi中有没有用来搜索文件的函数
 
给你一个完成的例子<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>&nbsp; Windows, Messages, SysUtils, Classes, Graphics, Controls,<br>&nbsp; Forms, Dialogs, StdCtrls, FileCtrl, Grids, Outline, DirOutln;<br><br>type<br>&nbsp; TMainForm = class(TForm)<br>&nbsp; &nbsp; dcbDrives: TDriveComboBox;<br>&nbsp; &nbsp; edtFileMask: TEdit;<br>&nbsp; &nbsp; lblFileMask: TLabel;<br>&nbsp; &nbsp; btnSearchForFiles: TButton;<br>&nbsp; &nbsp; lbFiles: TListBox;<br>&nbsp; &nbsp; dolDirectories: TDirectoryOutline;<br>&nbsp; &nbsp; procedure btnSearchForFilesClick(Sender: TObject);<br>&nbsp; &nbsp; procedure dcbDrivesChange(Sender: TObject);<br>&nbsp; private<br>&nbsp; &nbsp; FFileName: String;<br>&nbsp; &nbsp; function GetDirectoryName(Dir: String): String;<br>&nbsp; &nbsp; procedure FindFiles(APath: String);<br>&nbsp; end;<br><br>var<br>&nbsp; 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>&nbsp; directory containing the back-slash (/) as the last character. }<br>begin<br>&nbsp; if Dir[Length(Dir)]&lt;&gt; '/' then<br>&nbsp; &nbsp; Result := Dir+'/'<br>&nbsp; else<br>&nbsp; &nbsp; 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>&nbsp; file with a specified mask through the current directory and its<br>&nbsp; sub-directories. }<br>var<br>&nbsp; FSearchRec,<br>&nbsp; DSearchRec: TSearchRec;<br>&nbsp; FindResult: integer;<br><br>&nbsp; function IsDirNotation(ADirName: String): Boolean;<br>&nbsp; begin<br>&nbsp; &nbsp; Result := (ADirName = '.') or (ADirName = '..');<br>&nbsp; end;<br><br>begin<br>&nbsp; APath := GetDirectoryName(APath); // Obtain a valid directory name<br>&nbsp; { Find the first occurrence of the specified file name }<br>&nbsp; FindResult := FindFirst(APath+FFileName,faAnyFile+faHidden+<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; faSysFile+faReadOnly,FSearchRec);<br>&nbsp; try<br>&nbsp; &nbsp; { Continue to search for the files according to the specified<br>&nbsp; &nbsp; &nbsp; mask. If found, add the files and their paths to the listbox.}<br>&nbsp; &nbsp; while FindResult = 0 do<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; lbFiles.Items.Add(LowerCase(APath+FSearchRec.Name));<br>&nbsp; &nbsp; &nbsp; FindResult := FindNext(FSearchRec);<br>&nbsp; &nbsp; end;<br><br>&nbsp; &nbsp; { Now search the sub-directories of this current directory. Do this<br>&nbsp; &nbsp; &nbsp; by using FindFirst to loop through each subdirectory, then call<br>&nbsp; &nbsp; &nbsp; FindFiles (this function) again. This recursive process will<br>&nbsp; &nbsp; &nbsp; continue until all sub-directories have been searched. }<br>&nbsp; &nbsp; FindResult := FindFirst(APath+'*.*', faDirectory, DSearchRec);<br><br>&nbsp; &nbsp; while FindResult = 0 do<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; if ((DSearchRec.Attr and faDirectory) = faDirectory) and not<br>&nbsp; &nbsp; &nbsp; &nbsp; IsDirNotation(DSearchRec.Name) then<br>&nbsp; &nbsp; &nbsp; &nbsp; FindFiles(APath+DSearchRec.Name); // Recursion here<br>&nbsp; &nbsp; &nbsp; FindResult := FindNext(DSearchRec);<br>&nbsp; &nbsp; end;<br>&nbsp; finally<br>&nbsp; &nbsp; FindClose(FSearchRec);<br>&nbsp; end;<br>end;<br><br>procedure TMainForm.btnSearchForFilesClick(Sender: TObject);<br>{ This method starts the searching process. It first changes the cursor<br>&nbsp; to an hourglass since the process may take awhile. It then clears the<br>&nbsp; listbox and calls the FindFiles() function which will be called<br>&nbsp; recursively to search through sub-directories }<br>begin<br>&nbsp; Screen.Cursor := crHourGlass;<br>&nbsp; try<br>&nbsp; &nbsp; lbFiles.Items.Clear;<br>&nbsp; &nbsp; FFileName := edtFileMask.Text;<br>&nbsp; &nbsp; FindFiles(dolDirectories.Directory);<br>&nbsp; finally<br>&nbsp; &nbsp; Screen.Cursor := crDefault;<br>&nbsp; end;<br>end;<br><br>procedure TMainForm.dcbDrivesChange(Sender: TObject);<br>begin<br>&nbsp; dolDirectories.Drive := dcbDrives.Drive;<br>end;<br><br>end.<br>-------------------------------------------------------<br><br>
 
FindFirst<br>FindNext<br>FindClose<br>三个函数正是你要的<br>
 
一个递归。
 
后退
顶部