Board index » delphi » need help with gettin

need help with gettin

-=[ In:jle...@accessus.net was heard to say... ]=-

 In> i wanna know how i can get a dirlist of all dir's without klnowing
 In> what dir's are on that drive

This requires using FindFirst/FindNext, preferably in a recursive loop. You
need to start at the root and have a procedure that checks the file masks
of each file, and if it's a directory then put it's name in your list and
call the procedure again recursively. This will allow you to run through
every directory on the drive.

Here's how it should look (this is untested, so it may have errors):

{----------------------------------------------------}
PROCEDURE ScanDrive(Dir: string);
var
   DirInfo: SearchRec;

begin
   ChDir(Dir);
   FindFirst('*.*',$3F,DirInfo);
   while DosError = 0 do begin
      if (DirInfo.Attr and Directory) <> 0 then begin
         if DirInfo.Name[1] <> '.' then begin
            WriteLn(DirInfo.Name);
            ScanDrive(DirInfo.Name);
         end;
      end;
      FindNext(DirInfo);
   end;
end;
{----------------------------------------------------}

SearchRec is a special built-in Pascal record for use with FindFirst/FindNext
which gives info on the file passed to it. The file attributes are a little
confusing to use, and the directory attribute in particular, but if I've
written this correctly it should detect only directories. Check the manuals
for more information on file attributes.

The confusing part of this will be the recursion. Notice how the procedure
calls itself when a directory is found. At first you might think that this
wouldn't find every directory, but if you run this in the IDE and watch as it
scans your hard drive you'll see that when it has finished with a particular
subdirectory it continues where it left off in the parent directory before it
called itself, so in effect it will not miss anything.

I've written a program that uses the above recursive procedure and extends it
to allow me to search for files within ZIP'ed archives as well. And with the
addition of my own wildcard-matching routine I can search for all kinds of
file specs. It was a great way to learn about recursion, among other things.

  /-=oOo=--=oOo=--=oOo=--=oOo=--=oOo=--=oOo=--=oOo=--=oOo=--=oOo=--=oOo=-\
  $           Kim Forwood  <kim.forw...@access.cn.camriv.bc.ca>          %
  %              http://goodship.cn.camriv.bc.ca/~kforwood/              $
  $         For what purpose is life, if one cannot live freely?         %
  \-=oOo=--=oOo=--=oOo=--=oOo=--=oOo=--=oOo=--=oOo=--=oOo=--=oOo=--=oOo=-/
        WARNING: Flames and adverti{*word*224}ts sent to my email address
              may result in 20 gigabytes of garbage returned!

___ Blue Wave/QWK v2.20

 

Re:need help with gettin


On 11 Jan 1997 14:10:40 PST,  Kim.Forw...@access.cn.camriv.bc.ca

Quote
(Kim Forwood) wrote:

>-=[ In:jle...@accessus.net was heard to say... ]=-

> In> i wanna know how i can get a dirlist of all dir's without klnowing
> In> what dir's are on that drive

>This requires using FindFirst/FindNext, preferably in a recursive loop. You
>need to start at the root and have a procedure that checks the file masks
>of each file, and if it's a directory then put it's name in your list and
>call the procedure again recursively. This will allow you to run through
>every directory on the drive.

>Here's how it should look (this is untested, so it may have errors):

As you suggested, there are some logic errors.  Since jlewis
didn't know how to scan the directories on a drive, he may have
difficulty identifying the problem.

The first thing the routine does is change the directory.  Of
itself, this might not be a bad thing, but the directory is not
restored when the routine ends.  Since the recursive call only
passes the subdirectory, the ChDir of the first call after the
routine regains control will most likely result in "Path not
found".

Since saving and restoring the current path would require a
local string (more stack overhead) I suggest that the routine
use and pass the entire path.

Quote
>{----------------------------------------------------}
>PROCEDURE ScanDrive(Dir: string);
>var
>   DirInfo: SearchRec;

>begin

{>   ChDir(Dir);    eliminate this instruction }

    Case Dir[Length[Dir]) of
      ':', '\': {nop};
      Else Dir := Dir + '\';
    End{Case};

    FindFirst(Dir + '*.*', $3F, DirInfo);  {add Dir  ...red}

Quote
>   while DosError = 0 do begin
>      if (DirInfo.Attr and Directory) <> 0 then begin
>         if DirInfo.Name[1] <> '.' then begin
>            WriteLn(DirInfo.Name);

              ScanDrive(Dir + DirInfo.Name); {add Dir  ...red}

Quote
>         end;
>      end;
>      FindNext(DirInfo);
>   end;
>end;
>{----------------------------------------------------}

Of course you could add a backslash to Dir+DirInfo.Name when
making the recursive call, but that won't eliminate the need for
the case statement which would still be needed to insure the
initial dir string is properly terminated.

   ...red

Other Threads