Finds all files matching a file mask and attributes SysUtils unit
 function FindFirst(const FileMask string; Attributes Integer; var SearchResult TSearchRec):Integer;
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)
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
FileSearchSearch for a file in one or more directories
FindCloseCloses a successful FindFirst file search
FindNextFind the next file after a successful FindFirst
TSearchRecRecord used to hold data for FindFirst and FindNext
 Download this web site as a Windows program.

Example code : Find all Unit1.d* regular files in the current directory
  searchResult : TSearchRec;

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

    // Must free up resources used by these successful finds
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
  searchResult : TSearchRec;

  // Try to find directories above the current directory

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

    // Must free up resources used by these successful finds
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
Delphi Programming © Neil Moffatt . All rights reserved.  |  Home Page