遍历某路径下的所有文件
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; }
常记溪亭日暮,沉醉不知归路。兴尽晚回舟,误入藕花深处。争渡,争渡,惊起一滩鸥鹭。
昨夜雨疏风骤,浓睡不消残酒。试问卷帘人,却道海棠依旧。知否?知否?应是绿肥红瘦。