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