From: "Peter Below (TeamB)" <100113.1101@compuXXserve.com> Subject: Re: Directory search Date: 27 Jun 1999 00:00:00 GMT Message-ID: Content-Transfer-Encoding: 8bit References: <7l3a5m$i4j12@forums.borland.com> Content-Type: text/plain; charset=iso-8859-1 Organization: TeamB Mime-Version: 1.0 Reply-To: 100113.1101@compuXXserve.com Newsgroups: borland.public.delphi.winapi > I need directory search alcorithm. For example, I want to search for all the > files that match some criteria in some drive. > recursively scanning all drives { excerpt from form declaration, form has a listbox1 for the results, a label1 for progress, a button2 to start the scan, an edit1 to get the search mask from, a button3 to stop the scan. } private { Private declarations } FScanAborted: Boolean; public { Public declarations } Function ScanDrive( root, filemask: String; hitlist: TStrings ): Boolean; Function TForm1.ScanDrive( root, filemask: String; hitlist: TStrings ): Boolean; Function ScanDirectory( Var path: String ): Boolean; Var SRec: TSearchRec; pathlen: Integer; res: Integer; Begin label1.caption := path; pathlen:= Length(path); { first pass, files } res := FindFirst( path+filemask, faAnyfile, SRec ); If res = 0 Then try While res = 0 Do Begin hitlist.Add( path + SRec.Name ); res := FindNext(SRec); End; finally FindClose(SRec) end; Application.ProcessMessages; Result := not (FScanAborted or Application.Terminated); If not Result Then Exit; {second pass, directories} res := FindFirst( path+'*.*', faDirectory, SRec ); If res = 0 Then try While (res = 0) and Result Do Begin If ((Srec.Attr and faDirectory) = faDirectory) and (Srec.name <> '.') And (Srec.name <> '..') Then Begin path := path + SRec.name + '\'; Result := ScanDirectory( path ); SetLength( path, pathlen ); End; res := FindNext(SRec); End; finally FindClose(SRec) end; End; Begin FScanAborted := False; Screen.Cursor := crHourglass; try Result := ScanDirectory(root); finally Screen.Cursor := crDefault end; End; procedure TForm1.Button2Click(Sender: TObject); Var ch: Char; root: String; Begin root := 'C:\'; For ch := 'A' to 'Z' Do Begin root[1] := ch; Case GetDriveType( Pchar( root )) Of DRIVE_FIXED, DRIVE_REMOTE: If not ScanDrive( root, edit1.text, listbox1.items ) Then Break; End; End; end; procedure TForm1.Button3Click(Sender: TObject); begin // aborts scan fScanAborted := True; end; Peter Below (TeamB) 100113.1101@compuserve.com) No replies in private e-mail, please, unless explicitly requested!