community.borland.com

Article #15647: Using FindFirst and FindNext to find directories.

 Technical Information Database

TI647D.txt   Using FindFirst and FindNext to find directories.
Category   :General Programming
Platform    :All
Product    :Pascal  All

Description:
{
  Finding directories using Pascal's FindFirst() and
  FindNext() procedure can be a bit tricky.  By default,
  FindFirst() and FindNext() will find ALL files.  Not
  just directories.  To determine if what FindFirst()
  and FindNext() finds is really a directory, you must
  check the Attr field of the SearchRec to determine
  if the attribute indicates that the element is a
  directory.

  The following program adds only directory names to an
  array of strings.
}

program FindIt;

uses DOS;

const
  ArrayLimit = 512;

type
  DOSFileName = String[12];

var
  SR: SearchRec;
  A: array[1..ArrayLimit] of DOSFileName;
  i: integer;

begin
  i := 1;
  { find the first directory }
  FindFirst('c:\*.*', Directory, SR);
  { check for an error }
  If DosError = 0 then begin
    repeat
      { is it really a directory? }
      if (SR.Attr and Directory) <> 0 then begin
        A[i] := SR.Name;  { add dir to array }
        i := i + 1;       { increment index }
      end;
      FindNext(SR);       { find next directory }
    { exit loop if a DOS error occurs }
    until DosError <> 0;
  end
  else
    { Write error if FindFirst() fails }
    Writeln('Can''t do it.  Error code: ', DOSError);
end.


Reference:


7/16/98 4:33:49 PM
 

Last Modified: 01-SEP-99