访问目录文件夹下的文件是常常须要的操做,C/C++和win32接口都没有提供直接调用的函数。在这里总结了几个常常用到的函数,经过MFC的CFileFind函数递归遍历实现,包括如下几个功能函数:函数
//查找目录下全部的文件夹 void FindFolder(string dir, vector<string> &folderPath) { CFileFind fileFinder; CString filePath = CString(dir.c_str()) + _T("\\*.*"); BOOL bFinished = fileFinder.FindFile(filePath); while (bFinished) //每次循环对应一个类别目录 { bFinished = fileFinder.FindNextFile(); if (fileFinder.IsDirectory() && !fileFinder.IsDots()) //fileFinder.IsDots()识别"."文件和".."文件 { CString filePath = fileFinder.GetFilePath(); folderPath.push_back(filePath.GetBuffer()); filePath.ReleaseBuffer(); } } fileFinder.Close(); } //查找目录下全部的文件(不遍历目录的目录) void FindDirFileNoFormat(string dir, vector<string> &filePath) { CFileFind fileFinder; CString path = CString(dir.c_str()) + _T("\\*.*"); BOOL bFinished = fileFinder.FindFile(path); while (bFinished) //每次循环对应一个类别目录 { bFinished = fileFinder.FindNextFile(); if (fileFinder.IsDirectory() || fileFinder.IsDots()) //fileFinder.IsDots()识别"."文件和".."文件 { continue; } else { CString findPath = fileFinder.GetFilePath(); filePath.push_back(findPath.GetBuffer()); findPath.ReleaseBuffer(); } } fileFinder.Close(); } //查找目录下全部的文件(遍历目录的目录) void FindAllFileNoFormat(string dir, vector<string> &filePath) { CFileFind fileFinder; CString path = CString(dir.c_str()) + _T("\\*.*"); BOOL bFinished = fileFinder.FindFile(path); while (bFinished) //每次循环对应一个类别目录 { bFinished = fileFinder.FindNextFile(); // 跳过 . 和 .. ; 不然会陷入无限循环中!!! if (fileFinder.IsDots()) { continue; } // if (fileFinder.IsDirectory()) { CString findPath = fileFinder.GetFilePath(); string subdir = findPath.GetBuffer(); FindAllFileNoFormat(subdir, filePath); findPath.ReleaseBuffer(); } else { CString findPath = fileFinder.GetFilePath(); filePath.push_back(findPath.GetBuffer()); findPath.ReleaseBuffer(); } } fileFinder.Close(); } // 查找目录下某一类型文件 (不遍历目录的目录) void FindDirFile(string dir, string format, vector<string> &filePath) { CFileFind fileFinder; CString path = CString(dir.c_str()) + _T("\\*") + CString(format.c_str()); BOOL bFinished = fileFinder.FindFile(path); while (bFinished) //每次循环对应一个类别目录 { bFinished = fileFinder.FindNextFile(); if (fileFinder.IsDirectory() && !fileFinder.IsDots()) //fileFinder.IsDots()识别"."文件和".."文件 { continue; } else { CString findPath = fileFinder.GetFilePath(); filePath.push_back(findPath.GetBuffer()); findPath.ReleaseBuffer(); } } fileFinder.Close(); } //获得文件路径的格式后缀 string GetPathFormat(string filePath) { string format = filePath; size_t p = filePath.find_last_of('.'); if (p == -1) { return string(); } format.erase(0, p); return format; } // 查找目录下某一类型文件 (遍历目录的目录) void FindDirAllFileEx(string dir, vector<string> &format, vector<string>& filePath) { CFileFind fileFinder; CString path = CString(dir.c_str()) + _T("\\*.*"); BOOL bFinished = fileFinder.FindFile(path); while (bFinished) //每次循环对应一个类别目录 { bFinished = fileFinder.FindNextFile(); // 跳过 . 和 .. ; 不然会陷入无限循环中!!! if (fileFinder.IsDots()) { continue; } if (fileFinder.IsDirectory()) { CString findPath = fileFinder.GetFilePath(); string subdir = findPath.GetBuffer(); FindDirAllFileEx(subdir, format, filePath); findPath.ReleaseBuffer(); } else { //获取文件类型 CString findPath = fileFinder.GetFilePath(); string file = findPath.GetBuffer(); string postfix = GetPathFormat(file); bool flag = false; for (auto it : format) { if (_stricmp(it.c_str(), postfix.c_str()) == 0) { flag = true; break; } } if (flag) { filePath.push_back(file); } findPath.ReleaseBuffer(); } } fileFinder.Close(); }
有一点值得注意的是我这里函数的参数都封装成STL的string,在多字节下能够直接使用,在unicode下须要稍微修改下CString与string的转换。post