Searches for the first instance of a file name with a given set of attributes in a specified directory.
function FindFirst(const Path: string; Attr: Integer; var F: TSearchRec): Integer;
int FindFirst(const AnsiString Path, int Attr, TSearchRec F);
SysUtils
FindFirst searches the directory specified by Path for the first file that matches the file name implied by Path and the attributes specified by the Attr parameter. The result is returned in the F parameter. Use the fields of this search record to extract the information needed. FindFirst returns 0 if a file was successfully located, otherwise, it returns an error code.
The Path constant parameter is the directory and file name mask, including wildcard characters. For example, '.\test\*.*' specifies all files in the test subdirectory.
The Attr parameter specifies the special files to include in addition to all normal files. Choose from these file attribute constants when specifying the Attr parameter:
Constant |
Description |
Read-only files | |
Hidden files | |
System files | |
Volume ID files | |
Directory files | |
Archive files | |
Any file |
Attributes can be combined by adding (Delphi) or or-ing (C++) their constants or values. For example, to search for read-only and hidden files in addition to normal files, pass (faReadOnly + faHidden) in Delphi or (faReadOnly | faHidden) in C++ as the Attr parameter. To include only normal files, pass zero for the Attr parameter.
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). */ 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). } 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) 2008 CodeGear(TM). All Rights Reserved.
|
What do you think about this topic? Send feedback!
|