C++ Builder 参考手册 ➙ System::Sysutils ➙ FindFirst
查找第一个符合条件的文件
头文件:#include <System.SysUtils.hpp>
命名空间:System::Sysutils
函数原型:
int __fastcall FindFirst(const System::UnicodeString Path, int Attr, TSearchRec &F);
参数:
- Path:路径和文件名,支持通配符
*
和?
,例如"C:\\Windows\\*.exe"
- Attr:要查找的文件或文件夹的属性,faAnyFile 为没有限制,匹配任何属性的文件;
- F:返回第一个符合条件的文件或文件夹,TSearchRec 类型的结构体:
• Name 文件名
• Size 文件大小 (字节数)
• Attr 文件属性
• ExcludeAttr 返回本次查找不包含的属性,可能包含 faHidden、faSysFile 和 faDirectory,原因是参数 Attr 没有包含这些属性,查找的时候就没包含这样属性的文件;
• TimeStamp 文件的修改时间
• FindData 由操作系统返回的查找数据
• FindHandle 由操作系统返回的查找句柄
返回值:
- 0:找到符合条件的文件或文件夹,通过参数 F 返回找到的文件;
- 不等于 0:没找到符合条件的文件夹或文件夹
- 可以用函数 FindNext 继续查找下一个符合条件的文件
- 查找结束时,必须用函数 FindClose 结束查找,释放占用的资源
属性 | 描述 |
---|---|
faInvalid | 无效的文件,文件不会有这个属性值,是在获取属性时表示出错的 |
faReadOnly | 只读 |
faHidden | 隐藏 |
faSysFile | 系统 |
faVolumeID | 卷标 (过时的属性) |
faDirectory | 文件夹 |
faArchive | 归档,如果文件被修改这个属性会被置位。 新创建的文件一般都有这个属性, 把这个属性清掉之后,如果发现这个属性被置位了, 可以认为这个文件被修改了 |
faNormal | 文件没有其他属性,这个属性独立存在 |
faTemporary | 临时文件 |
faSymLink | 符号链接 |
faCompressed | 压缩 |
faEncrypted | 加密 |
faVirtual | 虚拟文件 |
faAnyFile | 文件不会有这个属性值,这是在查找文件时使用的值,表示任何属性的文件 |
例子:把 C:\Windows 文件夹里面的内容显示在 TStringGrid 里面
void __fastcall TForm1::Button1Click(TObject *Sender)
{
StringGrid1->RowCount = 2;
StringGrid1->Cells[0][0] = L"文件名";
StringGrid1->Cells[1][0] = L"大小";
StringGrid1->Cells[2][0] = L"时间";
StringGrid1->Cells[3][0] = L"属性";
int iRowNo = 1;
TSearchRec sr;
if(FindFirst(L"C:\\Windows\\*.*", faAnyFile, sr) == 0)
{
do{
UnicodeString sAttr;
sAttr += sr.Attr & faReadOnly ? L"r" : L"-";
sAttr += sr.Attr & faArchive ? L"a" : L"-";
sAttr += sr.Attr & faHidden ? L"h" : L"-";
sAttr += sr.Attr & faSysFile ? L"s" : L"-";
StringGrid1->RowCount = iRowNo + 1;
StringGrid1->FixedRows = 1;
StringGrid1->Cells[0][iRowNo] = sr.Name;
StringGrid1->Cells[1][iRowNo] = sr.Attr & faDirectory ? String(L"<文件夹>") : IntToStr(sr.Size);
StringGrid1->Cells[2][iRowNo] = FormatDateTime(L"yyyy/mm/dd hh:nn:ss.zzz",sr.TimeStamp);
StringGrid1->Cells[3][iRowNo] = sAttr;
iRowNo++;
} while(FindNext(sr) == 0);
FindClose(sr);
}
}
运行结果
相关:
- System::Sysutils::FindFirst
- System::Sysutils::FindNext
- System::Sysutils::FindClose
- System::Sysutils::TSearchRec
- System::Sysutils::FileSearch
- System::Sysutils::FileExists
- System::Sysutils::DirectoryExists
- System::Sysutils::FileAge
- System::Sysutils::FileGetDate
- System::Sysutils::FileGetDateTimeInfo
- System::Sysutils::FileSetDate
- System::Sysutils::FileGetAttr
- System::Sysutils::FileSetAttr
- System::Sysutils::FileIsReadOnly
- System::Sysutils::FileSetReadOnly
- System::Sysutils::CreateDir
- System::Sysutils::ForceDirectories
- System::Sysutils::RemoveDir
- System::Sysutils::GetCurrentDir
- System::Sysutils::SetCurrentDir
- System::Sysutils::DeleteFile
- System::Sysutils::RenameFile
- System::Sysutils::IsAssembly
- System::Sysutils::DiskFree
- System::Sysutils::DiskSize
- System::Sysutils::FileDateToDateTime
- System::Sysutils::DateTimeToFileDate
- System::Sysutils
C++ Builder 参考手册 ➙ System::Sysutils ➙ FindFirst