ObjectPascal
FindFirst
Function
Finds all files matching a file mask and attributes SysUtils unit
 function FindFirst ( const FileMask : string; Attributes : Integer; var SearchResult : TSearchRec ) : Integer;
Description
The FindFirst function searches for files matching a FileMask and Attributes, returning the first match (if found) in SearchResult.
 
The Attributes define files to search for in addition to regular files.
 
If a match is found, then the return value is 0, otherwise, it is negative (and the result record is not filled in).
 
The FileMask may contain a path, as well as a file name. The file name may contain wild cards:
 
: Match any one character
: Match 0, 1 or more characters

 
The Attributes may be set as follows:
 
faAnyFile  : Any file
faReadOnly  : Read-only files
faHidden  : Hidden files
faSysFile  : System files
faVolumeID  : Volume ID files
faDirectory  : Directory files
faArchive  : Archive files

 
You may set Attributes from one or more of the above by concatenating them.
 
The SearchResult record comprises many fields. Some are used by subsequent calls to FindNext. Others are available to your program :
 
Name  : Of the long name of the file found
Size  : The size of the file in bytes
Time  : Last modified date/time of the file
Attr  : The file attributes (as above)
Notes
Warning : you must call FindClose after a successful FindFirst when you have finished searching (finished calling FindNext). This frees up resources held by the find process (such as the SearchResult record).

If the FileMask contains no path information, then the search is in the current directory.

Because the Attributes parameter defines additional file types to search for, you should filter the results Attr value to select only the desired file types.
Related commands
FileSearch Search for a file in one or more directories
FindClose Closes a successful FindFirst file search
FindNext Find the next file after a successful FindFirst
TSearchRec Record used to hold data for FindFirst and FindNext
 
Example code : Find all Unit1.d* regular files in the current directory
var
  searchResult : TSearchRec;

begin
  // Try to find regular files matching Unit1.d* in the current dir
  if FindFirst('Unit1.d*', faAnyFile, searchResult) = 0 then
  begin
    repeat
      WriteLn('File name = '+searchResult.Name);
      WriteLn('File size = '+IntToStr(searchResult.Size));
    until FindNext(searchResult) <> 0;

    // Must free up resources used by these successful finds
    FindClose(searchResult);
  end;
end;
Show full unit code
   File name = Unit1.dcu
   File size = 4382
   File name = Uni1.dfm
   File size = 524
   File name = Uni1.ddp
   File size = 51
  
 
Example code : Find all directories above and including the current one
var
  searchResult : TSearchRec;

begin
  // Try to find directories above the current directory
  SetCurrentDir('..');

  if FindFirst('*', faDirectory, searchResult) = 0 then
  begin
    repeat
      // Only show directories
      if (searchResult.attr and faDirectory) = faDirectory
      then WriteLn('Directory = '+searchResult.Name);
    until FindNext(searchResult) <> 0;

    // Must free up resources used by these successful finds
    FindClose(searchResult);
  end;
end;
Show full unit code
   Directory = .
   Directory = ..
   Directory = Bin
   Directory = Help
   Directory = Projects
   Directory = Demos
   Directory = Lib
   Directory = Objrepos
   Directory = MergeModules
   Directory = Imports
   Directory = Source
   Directory = Rave5
   Directory = Ocx