Board index » delphi » Searching through a drive for files

Searching through a drive for files

How can I search through a drive for a file in Delphi 3 ??  The system I
want to search has thousands of nested directories, and I need to look in
each one for a specified file & if it's there open it and extract some data.

Jim

j.m...@net.ntl.com

 

Re:Searching through a drive for files


How can I search through a drive for a file in Delphi 3 ??  The system I
want to search has thousands of nested directories, and I need to look in
each one for a specified file & if it's there open it and extract some data.

Jim

j.m...@net.ntl.com

Re:Searching through a drive for files


How can I search through a drive for a file in Delphi 3 ??  The system I
want to search has thousands of nested directories, and I need to look in
each one for a specified file & if it's there open it and extract some data.

Jim

j.m...@net.ntl.com

Re:Searching through a drive for files


How can I search through a drive for a file in Delphi 3 ??  The system I
want to search has thousands of nested directories, and I need to look in
each one for a specified file & if it's there open it and extract some data.

Jim

j.m...@net.ntl.com

Re:Searching through a drive for files


How can I search through a drive for a file in Delphi 3 ??  The system I
want to search has thousands of nested directories, and I need to look in
each one for a specified file & if it's there open it and extract some data.

Jim

j.m...@net.ntl.com

Re:Searching through a drive for files


How can I search through a drive for a file in Delphi 3 ??  The system I
want to search has thousands of nested directories, and I need to look in
each one for a specified file & if it's there open it and extract some data.

Jim

j.m...@net.ntl.com

Re:Searching through a drive for files


Quote
Jim Main wrote in message <741a59$lb...@nclient5-gui.server.ntli.net>...
>How can I search through a drive for a file in Delphi 3 ??  The system I
>want to search has thousands of nested directories, and I need to look in
>each one for a specified file & if it's there open it and extract some

data.

Apologies for sending this six (!) times  - I swear I didn't - it was either
Outlook 98 or my service provide that did it!!!

Jim

Re:Searching through a drive for files


Quote
Jim Main wrote:

> How can I search through a drive for a file in Delphi 3 ??  The system I
> want to search has thousands of nested directories, and I need to look in
> each one for a specified file & if it's there open it and extract some data.

http://www.geocities.com/SiliconValley/2926/tp.html
chapter FAQ, article "Recursion example, program SUCHE".
Written for good old Pascal, but easily adaptable to Delphi,
replacing some vocabulary and adding the final FindClose.

Regards, Franz Glaser

Re:Searching through a drive for files


Quote
Jim Main wrote:

> Jim Main wrote in message <741a59$lb...@nclient5-gui.server.ntli.net>...
> >How can I search through a drive for a file in Delphi 3 ??  The system I
> >want to search has thousands of nested directories, and I need to look in
> >each one for a specified file & if it's there open it and extract some
> data.

> Apologies for sending this six (!) times  - I swear I didn't - it was either
> Outlook 98 or my service provide that did it!!!

> Jim

Okay. Don't panic... At least we've all got the message.

You need to have a look in the Delphi help files for FindFirst and
FindNext functions. If it's still not clear, then give us a shout. They
basically let you find both files and directories. The TSearchRec that
they use specifies the starting point for another FindFirst or FindNext
call, thus allowing you to search breadth or depth first as you please,
only look at various sorts of file / directority entries etc etc.

MH.

Re:Searching through a drive for files


In article <741fm2$ls...@nclient5-gui.server.ntli.net>, "Jim Main"

Quote
<j.m...@net.ntl.com> writes:
>How can I search through a drive for a file in Delphi 3 ??  The system I
>want to search has thousands of nested directories, and I need to look in
>each one for a specified file & if it's there open it and extract some data.

You need to use some recursive (calls itself) code ie (in pseudo-code) :-

procedure LookForFile(path)
begin
  While I can find a filename
    find first filename including directorynames
    if filename is a directory and filename is not this or parent directory
then
      LookForFile(path + filename) // filename is the directory name
    else
      Put filename in mylist (or whatever) of filenames
    end
    Look for next filename
  endwhile
end

If you want a real code sample then e-mail me.

Alan Lloyd
alangll...@aol.com

Re:Searching through a drive for files


Quote
Jim Main wrote:

> How can I search through a drive for a file in Delphi 3 ??  The system I
> want to search has thousands of nested directories, and I need to look in
> each one for a specified file & if it's there open it and extract some data.

The "FindFirst" and "FindNext" (and "FindClose") routines are used to
search for files.  They can also return directory-entries.

Be aware that the "attrs" you specify are "in addition to normal
files."  ALSO be aware that if you ask for directories, you're going to
get "." and ".." and you will need to ignore these.

There does seem to be a limit on how many scans can be going on at one
time.  But this can be solved by using a non-recursive approach to the
problem.  Use a TStringList as a push-down stack of directory-names as
you find them:

        (1)  Push the initial directory on the stack.
        (2)  WHILE stack is not empty do:
        (3)     Pop a name off the stack.
        (4)     Find the first entry in that directory.
        (5)     WHILE not at end of directory:
        (6)        If the name is a directory-entry:
        (7)           If it is "." or ".." then ignore it.
        (8)           Otherwise, add the name (fully-qualified) to the stack.
        (9)        Else:
        (10)          If it is the name you are looking for, process it.
        (11)       End-if.
        (12)    End-while.
        (13)    FindClose
        (14) End-while.

Re:Searching through a drive for files


Quote
Jim Main wrote:

> Apologies for sending this six (!) times  - I swear I didn't - it was either
> Outlook 98 or my service provide that did it!!!

I don't believe you..
 I don't believe you..
  I don't believe you..
   I don't believe you..
I don't believe you..
  I don't believe you..

Oops, sorry. Ok, I believe you then. Here is one old and simple Find
routine to check through all the selected drives.

I believe that this same kind of Find code would have been easy to
find using DejaNews. http://www.dejanews.com/home_ps.shtml
Try it next time, it will save you a lot of time. And apologies:-).

Markku Nevalainen

The Find code is used this way:
Var
 S:String;
begin
  S:= FindFile('MWBACKUP.EXE','CDE'); {Check C:, D: and E: if MWBACKUP.EXE file exists}

unit FindIt;

interface

uses
  SysUtils, WinCrt;

type
  TSearchScope = (ssEntireDisk, ssCurrentDir, ssCurrentDirAndBelow);

const
  {default property values}
  DefStopOnFirstMatch = true;
  DefSearchScope      = ssEntireDisk;

var
  FLastMatch: TFileName;
  StopOnFirstMatch,Stopped:Boolean;
  SearchScope: TSearchScope;

FUNCTION FindFile(FilNam, DriveList:String):String;
function IsValidDrive(Drive: Char): Boolean;
procedure SearchDirectory(FileMask:String; Path: TFileName);

{--------------}
 implementation
{--------------}

FUNCTION FindFile(FilNam,DriveList:String):String;
var
  SearchRec: TSearchRec;
  i:Integer;
begin
  for i := 1 to Length(DriveList) do
  if IsValidDrive(DriveList[i])
  then SearchDirectory(FilNam,DriveList[i]+':\');
  Result:=FLastMAtch;
end; {of GetPath}

function IsValidDrive(Drive: Char): Boolean;
var
  Str: String;
begin
  ChDir(Drive+':');
  Result := IOResult = 0;
end;

procedure SearchDirectory(FileMask:String; Path: TFileName);
function MakePath(Path,FileName:TFileName):TFileName;
begin
  Result := Path;
  if Result[Length(Result)] <> '\' then
    Result := Result + '\';
  Result := Result + FileName;
end;

var
  SearchRec: TSearchRec;
begin
  {if Assigned(FOnChangeDirectory) then
    FOnChangeDirectory(Self,Path);}
  { one pass for files... }
  if FindFirst(MakePath(Path,FileMask), faAnyFile, SearchRec) = 0 then
    repeat
      with SearchRec do
      if Attr <> faDirectory then
      begin
         FLastMatch := MakePath(Path,Name);
         Writeln(FlastMAtch);
         if StopOnFirstMatch then
           Stopped := true
         else
           {Application.ProcessMessages;}
      end
    until (FindNext(SearchRec) <> 0) or Stopped;
  FindClose(SearchRec);
  { ... and another for subdirectories }
  if (SearchScope <> ssCurrentDir) or Stopped then begin
    if FindFirst(MakePath(Path,'*.*'), faDirectory, SearchRec) = 0 then
      repeat
        with SearchRec do
          if (Name <> '.') and (Name <> '..') and (Attr = faDirectory) then
            SearchDirectory(FileMask,MakePath(Path,Name));
        {Application.ProcessMessages;} { we have to be gentle to the others apps }
      until (FindNext(SearchRec) <> 0) or Stopped;
    FindClose(SearchRec);
  end;
end;

end.

Re:Searching through a drive for files


Quote
Jim Main wrote:

> How can I search through a drive for a file in Delphi 3 ??  The system I
> want to search has thousands of nested directories, and I need to look in
> each one for a specified file & if it's there open it and extract some data.

> Jim

> j.m...@net.ntl.com

Jim
   I have culled the following from a D1 app I wrote.
   Pass a Directory and Filename.
   Returns the full path if found or '' if not.
   eg SearchPath( 'c:\', 'abc.doc');
   You will need to replace Findfist, FindNext, FindClose with D3
equivatents (ie 32bit)

function SearchPath( Dir, FileName: String): String;
var SearchRec: TSearchRec;
begin
   Result := '';
   if FindFirst( Dir + FileName, faAnyFile, SearchRec) < 0 then begin
      FindClose( SearchRec);
      if FindFirst( Dir + '*.*', faDirectory, SearchRec) >= 0 then
repeat begin
         if ( (SearchRec.Attr and faDirectory) > 0) and
            ( SearchRec.Name <> '.') and
            ( SearchRec.Name <> '..') then
               Result := SearchPath( Dir + SearchRec.Name + '\',
FileName);
      end until ( FindNext( SearchRec) < 0) or ( Result <> '');
      FindClose( SearchRec);
   end else begin
      FindClose( SearchRec);
      Result := Dir + FileName;
   end;
end;

--
Dave Ansell
Marconi Electronic Systems
Waterlooville, England

Re:Searching through a drive for files


Quote
Dave Ansell wrote:

>    Returns the full path if found or '' if not.

I should have said it searches the given directory and all its
sub-directories recursively.

--
Dave Ansell
Marconi Electronic Systems
Waterlooville, England

Other Threads