🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.pascal.delphi.misc
Article #405

Re: How to search file ?

#405
From: Dirk Claessens
Date: Sun, 23 May 1999 00:00
117 lines
4079 bytes
Chow Hoi Ka, Eric heeft geschreven in bericht
<374833A0.88F127CD@sftw.umac.mo>...
:Hello,
:
:How to search a certain file(*.*, *.dat, etc.) in local drives ???
:
:Would you please to give me an simple example ????
:
:


Eric,

See code below. It was posted some time ago by Peter Below of TeamB.

Regards,
Dirk Claessens
______________________________________________
E-mail will bounce back, unless directed to:
dirk DOT claessens AT village DOT uunet DOT be
______________________________________________



=================REPOST=======================================
> Is it possible to search through more than one drive and many
> directories to locate a file?  Lets just say I'm looking for the file
> "text.txt" and It may be in either drive c or drive e.
> Could someone please provide an example to accomplish this?
>
Nick,
i happen to have this code snippet around:
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[1] <> '.')
          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 e-mail responses, please, unless explicitely requested!
Technical Details
Message-ID: <7ibn70$40h$2@xenon.inbe.net>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <374833A0.88F127CD@sftw.umac.mo>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: fd817,173444787e3eadc0
X-Google-Attributes: gidfd817,public
X-Google-Thread: 1043cf,173444787e3eadc0
X-Google-Attributes: gid1043cf,public
X-Google-Thread: 10baee,32487ee6cec1373e
X-Google-Attributes: gid10baee,public
From: "Dirk Claessens" <will.bounce@back.com>
Subject: Re: How to search file ?
Date: 1999/05/23
Message-ID: <7ibn70$40h$2@xenon.inbe.net>
X-Deja-AN: 481514459
References: <374833A0.88F127CD@sftw.umac.mo>
Organization: UUNET Benelux (post does not reflect views of UUNET Benelux)
X-MimeOLE: Produced By Microsoft MimeOLE V4.71.1712.3
Newsgroups: comp.lang.pascal.delphi.misc,alt.comp.lang.borland-delphi,borland.public.delphi.objectpascal