Pascal Scripting: FindFirst

Prototype:

function FindFirst(const FileName: String; var FindRec: TFindRec): Boolean;

Description:

Retrieves information about the first file matching the wildcard specified by FileName. Returns True if successful.

TFindRec is defined as:

  TFindRec = record
    Name: String;               // name of the found file (no path)
    Attributes: LongWord;       // file attributes
    SizeHigh: LongWord;         // size of the file, upper 32 bits
    SizeLow: LongWord;          // size of the file, lower 32 bits
    CreationTime: TFileTime;    // time file was created
    LastAccessTime: TFileTime;  // time file was last accessed
    LastWriteTime: TFileTime;   // time file was last modified
    AlternateName: String;      // file's short name (empty if none)
    FindHandle: THandle;        // used internally
  end;

TFileTime is defined as:

  TFileTime = record
    dwLowDateTime: DWORD;
    dwHighDateTime: DWORD;
  end;

Valid file attributes are:

  FILE_ATTRIBUTE_READONLY
  FILE_ATTRIBUTE_HIDDEN
  FILE_ATTRIBUTE_SYSTEM
  FILE_ATTRIBUTE_DIRECTORY
  FILE_ATTRIBUTE_ARCHIVE
  FILE_ATTRIBUTE_DEVICE
  FILE_ATTRIBUTE_NORMAL
  FILE_ATTRIBUTE_TEMPORARY
  FILE_ATTRIBUTE_SPARSE_FILE
  FILE_ATTRIBUTE_REPARSE_POINT
  FILE_ATTRIBUTE_COMPRESSED
  FILE_ATTRIBUTE_OFFLINE
  FILE_ATTRIBUTE_NOT_CONTENT_INDEXED
  FILE_ATTRIBUTE_ENCRYPTED
Remarks:

If FindFirst returns True, call FindNext to enumerate the rest of the files, and then FindClose.

Example:

The following example counts the number of files in the Windows System directory.

var
  FilesFound: Integer;
  FindRec: TFindRec;
begin
  FilesFound := 0;
  if FindFirst(ExpandConstant('{sys}\*'), FindRec) then begin
    try
      repeat
        // Don't count directories
        if FindRec.Attributes and FILE_ATTRIBUTE_DIRECTORY = 0 then
          FilesFound := FilesFound + 1;
      until not FindNext(FindRec);
    finally
      FindClose(FindRec);
    end;
  end;
  MsgBox(IntToStr(FilesFound) + ' files found in the System directory.',
    mbInformation, MB_OK);
end;
See also:

FindNext
FindClose