给你一个我以前做过的查找软件的一部分。<br>这种查找的最好用多线程!:)<br>unit SrchU;<br><br>interface<br><br>uses Classes, StdCtrls,ComCtrls;<br><br>type<br> TSearchThread = class(TThread)<br> private<br> CaseSens: Boolean;<br> FileNames: Boolean;<br> Recurse: Boolean;<br> SearchStr: string;<br> SearchPath: string;<br> FileSpec: string;<br> AddStr: string;<br> FSearchFile: string;<br> procedure AddToList;<br> procedure DoSearch(const Path: string);<br> procedure FindAllFiles(const Path: string);<br> procedure FixControls;<br> procedure ScanForStr(const FName: string; var FileStr: string);<br> procedure SearchFile(const FName: string);<br> procedure SetSearchFile;<br> protected<br> procedure Execute; override;<br> public<br> constructor Create(CaseS, FName, Rec: Boolean; const Str, SPath,<br> FSpec: string);<br> destructor Destroy; override;<br> end;<br><br>implementation<br><br>uses SysUtils, StrUtils, Windows, Forms, Main,shellapi;<br><br>constructor TSearchThread.Create(CaseS, FName, Rec: Boolean; const Str,<br> SPath, FSpec: string);<br>begin<br> CaseSens := CaseS;<br> FileNames := FName;<br> Recurse := Rec;<br> SearchStr := Str;<br> SearchPath := AddBackSlash(SPath);<br> FileSpec := FSpec;<br> inherited Create(False);<br>end;<br><br>destructor TSearchThread.Destroy;<br>begin<br> FSearchFile := '';<br> Synchronize(SetSearchFile);<br> Synchronize(FixControls);<br> inherited Destroy;<br>end;<br><br>procedure TSearchThread.Execute;<br>begin<br> FreeOnTerminate := True;<br><br> Priority := TThreadPriority(MainForm.SearchPri);<br> if not CaseSens then SearchStr := UpperCase(SearchStr);<br> FindAllFiles(SearchPath);<br> if Recurse then<br> DoSearch(SearchPath);<br>end;<br><br>procedure TSearchThread.FixControls;<br>begin<br> MainForm.EnableSearchControls(True);<br> mainform.PB.Position :=0;<br>end;<br><br>procedure TSearchThread.SetSearchFile;<br>begin<br> MainForm.StatusBar.Panels[0].Text := '搜索到符合条件的文件有:'+inttostr(MainForm.lbFiles.Items.count)+'个';<br> MainForm.StatusBar.Panels[1].Text := FSearchFile;<br> mainform.PB.Max :=mainform.lbFiles.Items.Count;<br> mainform.PB.StepIt;<br>end;<br><br>procedure TSearchThread.AddToList;<br>var<br> Listitem:TlistItem;<br> PcAddStr
char;<br>begin<br> listitem:=mainform.lbFiles.Items.Add;<br> if ExtractFileExt(StrUpper(Pchar(AddStr)))='.TXT' then<br> Listitem.ImageIndex:=2;<br> listitem.SubItems.Add(ExtractFileName(AddStr));<br> listitem.SubItems.Add(AddStr);<br> PcAddStr:=Pchar(AddStr);<br> <br> if GetFileAttributes(PcAddStr)=FILE_ATTRIBUTE_READONLY then<br> Listitem.SubItems.Add('只读');<br> if GetFileAttributes(PcAddStr)=FILE_ATTRIBUTE_HIDDEN then<br> Listitem.SubItems.Add('隐藏');<br> if GetFileAttributes(PcAddStr)=FILE_ATTRIBUTE_ARCHIVE then<br> Listitem.SubItems.Add('存档');<br> if GetFileAttributes(PcAddStr)=FILE_ATTRIBUTE_HIDDEN+FILE_ATTRIBUTE_ARCHIVE then<br> Listitem.SubItems.Add('隐藏+存档');<br> if GetFileAttributes(PcAddStr)=FILE_ATTRIBUTE_READONLY+FILE_ATTRIBUTE_HIDDEN then<br> Listitem.SubItems.Add('只读+隐藏');<br> if GetFileAttributes(PcAddStr)=FILE_ATTRIBUTE_READONLY+FILE_ATTRIBUTE_ARCHIVE then<br> Listitem.SubItems.Add('只读+存档');<br> if GetFileAttributes(PcAddStr)=FILE_ATTRIBUTE_READONLY+FILE_ATTRIBUTE_ARCHIVE+FILE_ATTRIBUTE_HIDDEN then<br> Listitem.SubItems.Add('只读+存档+隐藏');<br>end;<br><br>procedure TSearchThread.ScanForStr(const FName: string;<br> var FileStr: string);<br>var<br> Marker: string[1];<br> FoundOnce: Boolean;<br> FindPos: integer;<br>begin<br> FindPos := Pos(SearchStr, FileStr);<br> FoundOnce := False;<br> while (FindPos <> 0) and not Terminated do<br> begin<br> if not FoundOnce then<br> begin<br> if FileNames then<br> Marker := ''<br> else<br> Marker := ':';<br> AddStr := Format('%s%s', [FName, Marker]);<br> Synchronize(AddToList);<br> FoundOnce := True;<br> end;<br> if FileNames then Exit;<br><br> AddStr := GetCurLine(FileStr, FindPos);<br> Synchronize(AddToList);<br> FileStr := Copy(FileStr, FindPos + Length(SearchStr),<br> Length(FileStr));<br> FindPos := Pos(SearchStr, FileStr);<br> end;<br>end;<br><br>procedure TSearchThread.SearchFile(const FName: string);<br>var<br> DataFile: THandle;<br> FileSize: Integer;<br> SearchString: string;<br>begin<br> FSearchFile := FName;<br> Synchronize(SetSearchFile);<br> try<br> DataFile := FileOpen(FName, fmOpenRead or fmShareDenyWrite);<br> if DataFile = 0 then raise Exception.Create('');<br> try<br> FileSize := GetFileSize(DataFile, nil);<br> SetLength(SearchString, FileSize);<br> FileRead(DataFile, Pointer(SearchString)^, FileSize);<br> finally<br> CloseHandle(DataFile);<br> end;<br> if not CaseSens then SearchString := UpperCase(SearchString);<br> ScanForStr(FName, SearchString);<br> except<br> on Exception do<br> begin<br> AddStr := Format('Error reading file: %s', [FName]);<br> Synchronize(AddToList);<br> end;<br> end;<br>end;<br><br>procedure TSearchThread.FindAllFiles(const Path: string);<br>var<br> SR: TSearchRec;<br>begin<br> if FindFirst(Path + FileSpec, faArchive+faHidden, SR) = 0 then<br> try<br> repeat<br> SearchFile(Path + SR.Name);<br> until (FindNext(SR) <> 0) or Terminated;<br> finally<br> SysUtils.FindClose(SR);<br> end;<br>end;<br><br>procedure TSearchThread.DoSearch(const Path: string);<br>var<br> SR: TSearchRec;<br>begin<br> if FindFirst(Path + '*.*', faDirectory, SR) = 0 then<br> try<br> repeat<br> if ((SR.Attr and faDirectory) <> 0) and (SR.Name[1] <> '.') and<br> not Terminated then<br> begin<br> FindAllFiles(Path + SR.Name + '/');<br> DoSearch(Path + SR.Name + '/');<br> end;<br> until (FindNext(SR) <> 0) or Terminated;<br> finally<br> SysUtils.FindClose(SR);<br> end;<br>end;<br><br>end.