The TSearchRec type defines file information searched for by a FindFirst or FindNext function call. If a file is found, the fields of the TSearchRec type parameter are modified to specify the found file.
Attr represents the file attributes of the file. Test Attr against the following attribute constants or values to determine if a file attribute matches the file's properties.
Constant |
Value |
Description |
Linux Meaning |
1 |
Read-only files |
Current user does not have write access. | |
2 |
Hidden files |
File name begins with ".". | |
4 |
System files |
File is socket, symbolic link, device file, or FIFO. | |
8 |
Volume ID files |
Not used. | |
16 |
Directory files |
Directory. | |
32 |
Archive files |
Not used. | |
64 |
Symbolic link |
File is a symbolic link. | |
71 |
Any file |
Any file. |
The faReadOnly constant has the same name as the enumerated value that is defined by the TFieldAttribute type. If both the SysUtils and the Db units are used in your source files, you must disambiguate by specifying the unit to qualify the use of faReadOnly. That is, write SysUtils.faReadOnly (Delphi) or SysUtils::faReadOnly (C++).
To test for an attribute, combine the value of the Attr field with the attribute constant with 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:
(SearchRec.Attr and faHidden) <> 0.
(SearchRec.Attr & faHidden) != 0.
Time contains the time stamp of the file. It can be converted to a TDateTime value using FileDateToDateTime.
Size contains the size of the file in bytes.
Name contains the base file name, including extension.
FindHandle is an internal handle used to track find state.
FindData (Windows only) contains additional information such as the file creation time, last access time, and both the long and short file names.
Mode (Linux only) is the file's permission mask.
C++ Examples:
/* The following example uses an edit control, a button, a string grid, and seven check boxes. The check boxes correspond to the seven possible file attributes. When the button is clicked, the path specified in the edit control is searched for files matching the checked file attributes. The names and sizes of the matching files are inserted into the string grid. The Path constant parameter is the directory and file name mask, including wildcard characters. For example, '.\test\*.*' specifies all files in the current directory. Click at least one box before clicking the button. */ void __fastcall TForm1::Button1Click(TObject *Sender) { TSearchRec sr; int iAttributes = 0; StringGrid1->RowCount = 1; iAttributes |= faReadOnly * CheckBox1->Checked; iAttributes |= faHidden * CheckBox2->Checked; iAttributes |= faSysFile * CheckBox3->Checked; iAttributes |= faVolumeID * CheckBox4->Checked; iAttributes |= faDirectory * CheckBox5->Checked; iAttributes |= faArchive * CheckBox6->Checked; iAttributes |= faAnyFile * CheckBox7->Checked; StringGrid1->RowCount = 0; if (FindFirst(Edit1->Text, iAttributes, sr) == 0) { do { if ((sr.Attr & iAttributes) == sr.Attr) { StringGrid1->RowCount = StringGrid1->RowCount + 1; StringGrid1->Cells[1][StringGrid1->RowCount-1] = sr.Name; StringGrid1->Cells[2][StringGrid1->RowCount-1] = IntToStr(sr.Size); } } while (FindNext(sr) == 0); FindClose(sr); } }
Delphi Examples:
{ The following example uses an edit control, a button, a string grid, and seven check boxes. The check boxes correspond to the seven possible file attributes. When the button is clicked, the path specified in the edit control is searched for files matching the checked file attributes. The names and sizes of the matching files are inserted into the string grid. The Path constant parameter is the directory and file name mask, including wildcard characters. For example, '.\test\*.*' specifies all files in the current directory. Click at least one box before clicking the button. } procedure TForm1.Button1Click(Sender: TObject); var sr: TSearchRec; FileAttrs: Integer; begin StringGrid1.RowCount := 1; if CheckBox1.Checked then FileAttrs := faReadOnly else FileAttrs := 0; if CheckBox2.Checked then FileAttrs := FileAttrs + faHidden; if CheckBox3.Checked then FileAttrs := FileAttrs + faSysFile; if CheckBox4.Checked then FileAttrs := FileAttrs + faVolumeID; if CheckBox5.Checked then FileAttrs := FileAttrs + faDirectory; if CheckBox6.Checked then FileAttrs := FileAttrs + faArchive; if CheckBox7.Checked then FileAttrs := FileAttrs + faAnyFile; with StringGrid1 do begin RowCount := 0; if SysUtils.FindFirst(Edit1.Text, FileAttrs, sr) = 0 then begin repeat if (sr.Attr and FileAttrs) = sr.Attr then begin RowCount := RowCount + 1; Cells[1,RowCount-1] := sr.Name; Cells[2,RowCount-1] := IntToStr(sr.Size); end; until FindNext(sr) <> 0; FindClose(sr); end; end; end;
Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
|
What do you think about this topic? Send feedback!
|