aihot  2017-05-24 18:36:31  OpenCV |   查看评论   

directory.cpp:

  1. #include <windows.h>  
  2. #include "directory.hpp"  
  3.   
  4. // reference: contrib/src/inputoutput.cpp (2.4.9)  
  5.   
  6. namespace fbc{  
  7.   
  8.     std::vector<std::string> Directory::GetListFiles(const std::string& path, const std::string & exten, bool addPath)  
  9.     {  
  10.         std::vector<std::string> list;  
  11.         list.clear();  
  12.         std::string path_f = path + "/" + exten;  
  13.         WIN32_FIND_DATAA FindFileData;  
  14.         HANDLE hFind;  
  15.   
  16.         hFind = FindFirstFileA((LPCSTR)path_f.c_str(), &FindFileData);  
  17.         if (hFind == INVALID_HANDLE_VALUE) {  
  18.             return list;  
  19.         } else {  
  20.             do {  
  21.                 if (FindFileData.dwFileAttributes == FILE_ATTRIBUTE_NORMAL ||  
  22.                     FindFileData.dwFileAttributes == FILE_ATTRIBUTE_ARCHIVE ||  
  23.                     FindFileData.dwFileAttributes == FILE_ATTRIBUTE_HIDDEN ||  
  24.                     FindFileData.dwFileAttributes == FILE_ATTRIBUTE_SYSTEM ||  
  25.                     FindFileData.dwFileAttributes == FILE_ATTRIBUTE_READONLY) {  
  26.                     char* fname;  
  27.                     fname = FindFileData.cFileName;  
  28.   
  29.                     if (addPath) {  
  30.                         list.push_back(path + "/" + std::string(fname));  
  31.                     } else {  
  32.                         list.push_back(std::string(fname));  
  33.                     }  
  34.                 }  
  35.             } while (FindNextFileA(hFind, &FindFileData));  
  36.   
  37.             FindClose(hFind);  
  38.         }  
  39.   
  40.         return list;  
  41.     }  
  42.   
  43.     std::vector<std::string> Directory::GetListFilesR(const std::string& path, const std::string & exten, bool addPath)  
  44.     {  
  45.         std::vector<std::string> list = Directory::GetListFiles(path, exten, addPath);  
  46.         std::vector<std::string> dirs = Directory::GetListFolders(path, exten, addPath);  
  47.   
  48.         std::vector<std::string>::const_iterator it;  
  49.         for (it = dirs.begin(); it != dirs.end(); ++it) {  
  50.             std::vector<std::string> cl = Directory::GetListFiles(*it, exten, addPath);  
  51.             list.insert(list.end(), cl.begin(), cl.end());  
  52.         }  
  53.   
  54.         return list;  
  55.     }  
  56.   
  57.     std::vector<std::string> Directory::GetListFolders(const std::string& path, const std::string & exten, bool addPath)  
  58.     {  
  59.         std::vector<std::string> list;  
  60.         std::string path_f = path + "/" + exten;  
  61.         list.clear();  
  62.   
  63.         WIN32_FIND_DATAA FindFileData;  
  64.         HANDLE hFind;  
  65.   
  66.         hFind = FindFirstFileA((LPCSTR)path_f.c_str(), &FindFileData);  
  67.         if (hFind == INVALID_HANDLE_VALUE) {  
  68.             return list;  
  69.         } else {  
  70.             do {  
  71.                 if (FindFileData.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY &&  
  72.                     strcmp(FindFileData.cFileName, ".") != 0 &&  
  73.                     strcmp(FindFileData.cFileName, "..") != 0) {  
  74.                     char* fname;  
  75.                     fname = FindFileData.cFileName;  
  76.   
  77.                     if (addPath) {  
  78.                         list.push_back(path + "/" + std::string(fname));  
  79.                     } else {  
  80.                         list.push_back(std::string(fname));  
  81.                     }  
  82.                 }  
  83.             } while (FindNextFileA(hFind, &FindFileData));  
  84.   
  85.             FindClose(hFind);  
  86.         }  
  87.   
  88.         return list;  
  89.     }  
  90. }  

 

除特别注明外,本站所有文章均为 赢咖4注册 原创,转载请注明出处来自OpenCV代码提取:遍历指定目录下指定文件的实现

留言与评论(共有 0 条评论)
   
验证码:
[lianlun]1[/lianlun]