1.Windows API
#include <Windows.h> int main() { WIN32_FIND_DATA FindFileData = { 0 }; HANDLE h = FindFirstFile(TEXT("F:\BBC记录片\*.*"), &FindFileData); if (INVALID_HANDLE_VALUE == h) return -1; BOOL bRet = 1; int count = 0; while (bRet) { std::wstring sFilename = FindFileData.cFileName; bRet = FindNextFile(h, &FindFileData); ++count; } return 0; }
2. MFC中的CFileFind类
void CTestMFCDlg::OnCfilefind() { // TODO: Add your control notification handler code here CListBox* pList = (CListBox*)GetDlgItem(IDC_LIST_FILE); CFileFind finder; BOOL bRet = finder.FindFile("C:\*.*"); while(bRet) { bRet = finder.FindNextFile(); pList->AddString(finder.GetFileName()); } }
下面参考:https://blog.csdn.net/hisinwang/article/details/45725319
CFileFind fFinder; BOOL bFind = fFinder.FindFile(TEXT("D:/*.*")); while (bFind) { bFind = fFinder.FindNextFile(); //当前文件夹及上层文件夹(名称分别为..)----------------- if (fFinder.IsDots()) { continue; } //子文件夹--------------------------------------------- if(fFinder.IsDirectory()) { CString cstrDirName = fFinder.GetFileName(); //directory name CString cstrDirPath = fFinder.GetFilePath(); //directory path continue; } //文件------------------------------------------------- CString cstrFileName = fFinder.GetFileName(); //file name CString cstrFilePath = fFinder.GetFilePath(); //file path } fFinder.Close();
3. C语言函数 _findfirst
#include <io.h> #include <vector> int main() { _finddata64i32_t findData = { 0 }; long handle = _findfirst("F:\BBC记录片\*.*", &findData); if (-1 == handle) return 0; std::vector<std::string> vecFilename; vecFilename.push_back(findData.name); while (-1 != _findnext(handle,&findData)) { vecFilename.push_back(findData.name); } return 0; }