Previous: FExpand function To the Table of Content Next: FindNext procedure

- 4.24.2.11 -
Standard Units
Dos Unit
Dos Unit Procedures and Functions

FindFirst procedure

Targets: MS-DOS, OS/2, Win32


Dos Unit, WinDos Unit

Searches the specified directory for the matching file.

Declaration:
procedure FindFirst(Path: PChar; Attr: Word;
                    var F: TSearchRec);
Remarks:
Path is the drive and directory to search in and the file name to search for. Wildcards are allowed, for instance, 'MYFILE??.*'.

Attr contains the file attributes to include in the search in addition to all normal files.

FindFirst is used in conjunction with FindNext. Use FindNext to locate any addition files matching the search criteria. All errors are reported in DosError, which is a variable defined in the Dos unit.

See also:
FSearch

Example:
program DirList;
uses
  Dos;
var
  TotalDirCnt: Longint;
procedure List(Path : String);
var
  DirSearchRec: SearchRec;
begin
  if (Path[Length(Path)] <> '\') then
    Path := Path + '\';
  FindFirst(Path + '*.*', AnyFile, DirSearchRec);
  while DosError = 0 do
    begin
      if (DirSearchRec.Name <> '.') and
         (DirSearchRec.Name <> '..') and
         ((DirSearchRec.Attr and Directory) <> 0)
         then
        begin
          Inc(TotalDirCnt);
          Writeln(Path + DirSearchRec.Name);
          List(Path + DirSearchRec.Name);
        end;
      FindNext(DirSearchRec);
    end;
  end;
 
begin
  TotalDirCnt := 0;
  List('C:\');
  Writeln;
  Writeln('Total number of directories = ', TotalDirCnt);
end.



Previous: FExpand function To the Table of Content Next: FindNext procedure
FExpand function Table of Content FindNext procedure

- 4.24.2.11 -