Hi
I found thiis code - can't vouch for it, but you might want to give it a
shot:
---
Find a file
The following procedure locates all occurrences of a given file and adds
the complete path to a stringlist.
Note that recursion is used: FindFiles calls itself at the end of the
procedure.
Before calling FindFiles, the stringlist has to be created; afterwards,
you must free the stringlist.
In StartDir you pass the starting directory, including the disk drive.
In FileMask you pass the name of the
file to find, or a file mask. Examples:
FindFiles('c:\', 'letter01.doc')
FindFiles('d:\', 'euroen??.dpr')
FindFiles('d:\projects', '*.dpr')
In order to test the procedure, you add some components to the form: two
Edits, a Button, a TLabel and a
ListBox.
implementation
....
var
FilesList: TStringList;
...
procedure FindFiles(StartDir, FileMask: string);
var
SR: TSearchRec;
DirList: TStringList;
IsFound: Boolean;
i: integer;
begin
if StartDir[length(StartDir)] <> '\' then
StartDir := StartDir + '\';
{ Build a list of the files (not directories)
in directory StartDir }
IsFound :=
FindFirst(StartDir+FileMask, faAnyFile-faDirectory, SR) = 0;
while IsFound do begin
FilesList.Add(StartDir + SR.Name);
IsFound := FindNext(SR) = 0;
end;
FindClose(SR);
// Build a list of subdirectories
DirList := TStringList.Create;
IsFound := FindFirst(StartDir+'*.*', faAnyFile, SR) = 0;
while IsFound do begin
if ((SR.Attr and faDirectory) <> 0) and
(SR.Name[1] <> '.') then
DirList.Add(StartDir + SR.Name);
IsFound := FindNext(SR) = 0;
end;
FindClose(SR);
// Scan the list of subdirectories
for i := 0 to DirList.Count-1 do
FindFiles(DirList[i], FileMask);
DirList.Free;
end;
procedure TForm1.ButtonFindClick(Sender: TObject);
begin
FilesList := TStringList.Create;
FindFiles(EditStartDir.Text, EditFileMask.Text);
ListBox1.Items.Assign(FilesList);
LabelCount.Caption := 'Files found: ' + IntToStr(FilesList.Count);
FilesList.Free;
end;
For a more robust code, you should at minimum check if the limit of the
stringlist is not exceeded before
you add to it (or use a try...except construction). Somebody might look
for *.* starting in C:\
In that case, it's best to inform the user with a warning: "Too many
files. Not all occurences are listed."
-----
Quote
Zuljin wrote:
> Greetings ppL,
> I'm in the middle of creating a search-program similar to the one that's
> included in Windows. But I have a problem, I don't know how to make it
> search the _entire_ C:-drive, instead of a directory the user defines. This
> is my codes:
> procedure TSoegForm.FindPhiles(const startDirectory:string);
> begin
> res:=FindFirst(startDirectory+SearchField.Text,faAnyFile, SR);
> while res=0 do begin
> if (not ((SR.Attr and faDirectory)=faDirectory))
> and (SR.Name<>'..')
> and (SR.Name<>'.')
> then begin
> Results.Items.Add(StartDirectory+SR.Name);
> end;
> res:=FindNext(SR);
> end;
> FindClose(SR);
> end;
> This is the code that, when activated, searchs in the directory defined in
> FindPhiles. The files to search for is defined in the SearchField, which is
> an edit box.
> begin
> FindPhiles(SoegFormSoegEditBox.Text);
> end;
> This is assigned to a button, and when clicked, it gives procedure
> TSoegForm.FindPhiles the directory input, and starts to search after the
> defined file in the defined directory. But how should the code look like, if
> the search-function simply was to search the entire harddisk, included
> subdirectories ?
> I surely hope you understand what I mean here.
> - Live long and prosper
> Zuljin
--
Ken Masters
University of Cape Town
Cape Town, South Africa
Computer-Based Education? See http://www.uct.ac.za/projects/cbe/
----------------------------------------------------------------