RAD Studio |  |

Finding a File
See Also
Collapse All

There are three routines used for finding a file: FindFirst, FindNext, and FindClose. FindFirst searches for the first instance of a filename with a given set of attributes in a specified directory. FindNext returns the next entry matching the name and attributes specified in a previous call to FindFirst. FindClose releases memory allocated by FindFirst. You should always use FindClose to terminate a FindFirst/FindNext sequence. If you want to know if a file exists, a FileExists function returns True if the file exists, False otherwise.

The three file find routines take a TSearchRec as one of the parameters. TSearchRec defines the file information searched for by FindFirst or FindNext. If a file is found, the fields of the TSearchRec type parameter are modified to describe the found file.

Copy Code

    type
      TFileName = string;
      TSearchRec = record
        Time: Integer;//Time contains the time stamp of the file.
        Size: Integer;//Size contains the size of the file in bytes.
        Attr: Integer;//Attr represents the file attributes of the file.
        Name: TFileName;//Name contains the filename and extension.
        ExcludeAttr: Integer;
        FindHandle: THandle;
        FindData: TWin32FindData;//FindData contains additional information such as
        //file creation time, last access time, long and short filenames.
      end;

Copy Code

    struct TSearchRec
    {
    int Time; // time stamp of the file
    int Size; // size of the file in bytes
    int Attr; // file attribute flags
    AnsiString Name; // filename and extension
    int ExcludeAttr; // file attribute flags for files to ignore
    unsigned FindHandle;
    _WIN32_FIND_DATAA FindData; // structure with addition information
    } ;

On field of TSearchRec that is of particular interest is the Attr field. You can test Attr against the following attribute constants or values to determine if a file has a specific attribute:

Attribute constants and values

Constant | Value | Description
faReadOnly | $00000001 | Read-only files
faHidden | $00000002 | Hidden files
faSysFile | $00000004 | System files
faVolumeID | $00000008 | Volume ID files
faDirectory | $00000010 | Directory files
faArchive | $00000020 | Archive files
faAnyFile | $0000003F | Any file

To test for an attribute, combine the value of the Attr field with the attribute constant using the and operator. If the file has that attribute, the result will be greater than 0. For example, if the found file is a hidden file, the following expression will evaluate to True:

Copy Code

    (SearchRec.Attr and faHidden > 0).

Copy Code

    (SearchRec.Attr & faHidden > 0).

Attributes can be combined by OR'ing their constants or values. For example, to search for read-only and hidden files in addition to normal files, pass the following as the Attr parameter.

Copy Code

    (faReadOnly or faHidden).

Copy Code

    (faReadOnly | faHidden).

The following example illustrates the use of the three file find routines. It uses a label, a button named Search, and a button named Again on a form. When the user clicks the Search button, the first file in the specified path is found, and the name and the number of bytes in the file appear in the label's caption. Each time the user clicks the Again button, the next matching filename and size is displayed in the label:

Copy Code

    var
      SearchRec: TSearchRec;
    procedure TForm1.SearchClick(Sender: TObject);
    begin
      FindFirst('c:\Program Files\MyProgram\bin\

    *.*', faAnyFile, SearchRec);
      Label1.Caption := SearchRec.Name + ' is ' + IntToStr(SearchRec.Size) + " bytes in size';
    end;
    procedure TForm1.AgainClick(Sender: TObject);
    begin
      if FindNext(SearchRec) = 0 then
        Label1.Caption := SearchRec.Name + ' is ' + IntToStr(SearchRec.Size) + ' bytes in size'
      else
        FindClose(SearchRec);
    end;

Copy Code

      TSearchRec SearchRec; // global variable
    void __fastcall TForm1::SearchClick(TObject *Sender)
    {
      FindFirst("c:\\Program Files\\MyProgram\\bin\\
    *.*", faAnyFile, SearchRec);
      Label1->Caption = SearchRec->Name + " is " + IntToStr(SearchRec.Size) + " bytes in size";
    }
    void __fastcall TForm1::AgainClick(TObject *Sender)
    {
      if (FindNext(SearchRec) == 0)
        Label1->Caption = SearchRec->Name + " is " + IntToStr(SearchRec.Size) + " bytes in size";
      else
        FindClose(SearchRec);
    }

Copy Code

Note: In cross-platform applications, you should replace any hard-coded pathnames with the correct pathname for the system or use environment variables (on the Environment Variables page when you choose Tools->Options->Environment Options
) to represent them.
See Also

Deleting a File

Renaming a File

File Date-time Routines

Copying a File

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!