findfile.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. #include "pch.h"
  2. #include "findfile.h"
  3. #include <algorithm>
  4. // 查找文件的最大数量限制在nMaxLimit;
  5. CONST size_t nMaxLimit = 0xFFFFFFFF; //--4294967295;
  6. // 查找文件的默认数量限制在nDefLimit;
  7. CONST size_t nDefLimit = 0xFFFFF; //--1048575
  8. // 路径分隔符;
  9. CONST TCHAR c_pathSeparator = _T('\\');
  10. CONST TString s_pathSeparator = _T("\\");
  11. CONST TString g_sVertical = _T("|");
  12. // 正确的扩展名及格式样列;
  13. // 这个值最好可以从配置文件ini或xml中获取,不用在代码里写死;
  14. CONST TString g_sCorrectExt = _T("*.jpg|*.jpeg|*.png|*.bmp|*.cr2|*.nef|*.raw");
  15. findfile::findfile(void):
  16. m_nlimit(nDefLimit),
  17. m_pvtfiles(NULL),
  18. m_pvtnames(NULL),
  19. m_pvtfolders(NULL),
  20. m_pvtfiles_sth(NULL),
  21. m_pvtfiles_mth(NULL)
  22. {
  23. }
  24. findfile::~findfile(void)
  25. {
  26. }
  27. /************************************************************************/
  28. /*
  29. 函数:match
  30. 描述:判断指定的文件命名是否匹配指定的扩展名;
  31. 参数:
  32. IN sFileExt 扩展名;
  33. IN sFile 文件名;
  34. 返回:匹配返回非0值,否则返回0;
  35. 注意:
  36. */
  37. /************************************************************************/
  38. void findfile::setlimit(IN CONST INT &nLimit)
  39. {
  40. if (nLimit < 1)
  41. m_nlimit = nDefLimit;
  42. if (nLimit < nMaxLimit)
  43. m_nlimit = nLimit;
  44. }
  45. // 获取文件名;
  46. TString findfile::getfilename(IN CONST TString &file)
  47. {
  48. TString name;
  49. TString strfile = file;
  50. int nIndex = strfile.find_last_of(_T('\\')); // 如果file不包含 '\\' ,得不到文件名;
  51. if (nIndex == TString::npos)
  52. {
  53. nIndex = strfile.find_last_of(_T('.'));
  54. if ( nIndex == TString::npos )
  55. return _T("");
  56. return strfile.substr(0, nIndex);
  57. }
  58. name = strfile.substr(nIndex+1);
  59. nIndex = name.find_last_of(_T('.'));
  60. if (nIndex == TString::npos)
  61. return _T("");
  62. return name.substr(0, nIndex);
  63. }
  64. void findfile::find1st(TString folder, TString &file)
  65. {
  66. TString path = folder;
  67. if (path.size() > 0 && c_pathSeparator != path[path.size() - 1])
  68. path.append(s_pathSeparator);
  69. TString _file = _T("*");
  70. TString s = path + _file;
  71. WIN32_FIND_DATA fileinfo = { 0 };
  72. HANDLE handle = FindFirstFile(s.c_str(), &fileinfo);
  73. if (NULL != handle && INVALID_HANDLE_VALUE != handle)
  74. {
  75. do
  76. {
  77. // '.'和 '..'的系统文件去除;
  78. if (_T('.') != fileinfo.cFileName[0])
  79. {
  80. if ((FILE_ATTRIBUTE_DIRECTORY & fileinfo.dwFileAttributes) != FILE_ATTRIBUTE_DIRECTORY) // 目录;
  81. {
  82. file = path + fileinfo.cFileName;
  83. break;
  84. }
  85. }
  86. } while (FindNextFile(handle, &fileinfo));
  87. FindClose(handle);
  88. }
  89. }
  90. // 全部;
  91. void findfile::findall(IN CONST TString& folder)
  92. {
  93. TString path = folder;
  94. if (path.size() > 0 && c_pathSeparator != path[path.size() - 1])
  95. path.append(s_pathSeparator);
  96. TString file = _T("*");
  97. TString s = path + file;
  98. WIN32_FIND_DATA fileinfo = { 0 };
  99. HANDLE handle = FindFirstFile(s.c_str(), &fileinfo);
  100. if (NULL != handle && INVALID_HANDLE_VALUE != handle)
  101. {
  102. do
  103. {
  104. // 检查是否超过最大数;
  105. if (checklimit()) break;
  106. // '.'和 '..'的系统文件去除;
  107. if (_T('.') != fileinfo.cFileName[0])
  108. {
  109. if ((FILE_ATTRIBUTE_DIRECTORY & fileinfo.dwFileAttributes) == FILE_ATTRIBUTE_DIRECTORY) // 目录;
  110. {
  111. m_pvtfolders->push_back(path + fileinfo.cFileName);
  112. findall(path + fileinfo.cFileName);
  113. }
  114. else
  115. {
  116. if (!checklimit())
  117. {
  118. m_pvtnames->push_back(fileinfo.cFileName);
  119. m_pvtfiles->push_back(path + fileinfo.cFileName);
  120. }
  121. }
  122. }
  123. } while (FindNextFile(handle, &fileinfo));
  124. FindClose(handle);
  125. }
  126. }
  127. void findfile::findsubfolder(IN CONST TString& folder) // 查找子目录;
  128. {
  129. TString path = folder;
  130. if (path.size() > 0 && c_pathSeparator != path[path.size() - 1])
  131. path.append(s_pathSeparator);
  132. TString file = _T("*");
  133. TString s = path + file;
  134. WIN32_FIND_DATA fileinfo = { 0 };
  135. HANDLE handle = FindFirstFile(s.c_str(), &fileinfo);
  136. if (NULL != handle && INVALID_HANDLE_VALUE != handle)
  137. {
  138. do
  139. {
  140. if (_T('.') != fileinfo.cFileName[0])// '.'和 '..'的系统文件去除;
  141. {
  142. if ((FILE_ATTRIBUTE_DIRECTORY & fileinfo.dwFileAttributes) == FILE_ATTRIBUTE_DIRECTORY) // 目录;
  143. {
  144. m_pvtfolders->push_back(path + fileinfo.cFileName);
  145. }
  146. }
  147. } while (FindNextFile(handle, &fileinfo));
  148. FindClose(handle);
  149. }
  150. }
  151. void findfile::findallsubfolder(IN CONST TString& folder)
  152. {
  153. TString path = folder;
  154. if (path.size() > 0 && c_pathSeparator != path[path.size() - 1])
  155. path.append(s_pathSeparator);
  156. TString file = _T("*");
  157. TString s = path + file;
  158. WIN32_FIND_DATA fileinfo = { 0 };
  159. HANDLE handle = FindFirstFile(s.c_str(), &fileinfo);
  160. if (NULL != handle && INVALID_HANDLE_VALUE != handle)
  161. {
  162. do
  163. {
  164. if (_T('.') != fileinfo.cFileName[0])// '.'和 '..'的系统文件去除;
  165. {
  166. if ((FILE_ATTRIBUTE_DIRECTORY & fileinfo.dwFileAttributes) == FILE_ATTRIBUTE_DIRECTORY) // 目录;
  167. {
  168. m_pvtfolders->push_back(path + fileinfo.cFileName);
  169. findallsubfolder(path + fileinfo.cFileName); // 不入子目录查找;
  170. }
  171. }
  172. } while (FindNextFile(handle, &fileinfo));
  173. FindClose(handle);
  174. }
  175. }
  176. //////////////////////////////////////////////////////////////////////////
  177. void findfile::findfiles_findin_subfolder(IN CONST TString& folder)
  178. {
  179. TString path = folder;
  180. if (path.size() > 0 && c_pathSeparator != path[path.size() - 1])
  181. path.append(s_pathSeparator);
  182. TString file = _T("*");
  183. TString s = path + file;
  184. WIN32_FIND_DATA fileinfo = { 0 };
  185. HANDLE handle = FindFirstFile(s.c_str(), &fileinfo);
  186. if (NULL != handle && INVALID_HANDLE_VALUE != handle)
  187. {
  188. do
  189. {
  190. // 检查是否超过最大数;
  191. if (checklimit()) break;
  192. // '.'和 '..'的系统文件去除;
  193. if (_T('.') != fileinfo.cFileName[0])
  194. {
  195. if ((FILE_ATTRIBUTE_DIRECTORY & fileinfo.dwFileAttributes) == FILE_ATTRIBUTE_DIRECTORY) // 目录;
  196. {
  197. findfiles_findin_subfolder(path + fileinfo.cFileName);
  198. }
  199. else
  200. {
  201. if (!checklimit())
  202. {
  203. m_pvtfiles->push_back(path + fileinfo.cFileName);
  204. }
  205. }
  206. }
  207. } while (FindNextFile(handle, &fileinfo));
  208. FindClose(handle);
  209. }
  210. }
  211. void findfile::findfiles_findout_subfolder(IN CONST TString& folder)
  212. {
  213. TString path = folder;
  214. if (path.size() > 0 && c_pathSeparator != path[path.size() - 1])
  215. path.append(s_pathSeparator);
  216. TString file = _T("*");
  217. TString s = path + file;
  218. WIN32_FIND_DATA fileinfo = { 0 };
  219. HANDLE handle = FindFirstFile(s.c_str(), &fileinfo);
  220. if (NULL != handle && INVALID_HANDLE_VALUE != handle)
  221. {
  222. do
  223. {
  224. // 检查是否超过最大数;
  225. if (checklimit()) break;
  226. // '.'和 '..'的系统文件去除;
  227. if (_T('.') != fileinfo.cFileName[0])
  228. {
  229. if ((FILE_ATTRIBUTE_DIRECTORY & fileinfo.dwFileAttributes) != FILE_ATTRIBUTE_DIRECTORY) // 目录;
  230. {
  231. if (!checklimit())
  232. {
  233. m_pvtfiles->push_back(path + fileinfo.cFileName);
  234. }
  235. }
  236. }
  237. } while (FindNextFile(handle, &fileinfo));
  238. FindClose(handle);
  239. }
  240. }
  241. void findfile::findfiles_within_subfolder(IN CONST TString& folder)
  242. {
  243. TString path = folder;
  244. if (path.size() > 0 && c_pathSeparator != path[path.size() - 1])
  245. path.append(s_pathSeparator);
  246. TString file = _T("*");
  247. TString s = path + file;
  248. WIN32_FIND_DATA fileinfo = { 0 };
  249. HANDLE handle = FindFirstFile(s.c_str(), &fileinfo);
  250. if (NULL != handle && INVALID_HANDLE_VALUE != handle)
  251. {
  252. do
  253. {
  254. // 检查是否超过最大数;
  255. if (checklimit()) break;
  256. // '.'和 '..'的系统文件去除;
  257. if (_T('.') != fileinfo.cFileName[0])
  258. {
  259. if ((FILE_ATTRIBUTE_DIRECTORY & fileinfo.dwFileAttributes) == FILE_ATTRIBUTE_DIRECTORY) // 目录;
  260. {
  261. m_pvtfolders->push_back(path + fileinfo.cFileName);
  262. findfiles_within_subfolder(path + fileinfo.cFileName);
  263. }
  264. else
  265. {
  266. if (!checklimit())
  267. {
  268. m_pvtfiles->push_back(path + fileinfo.cFileName);
  269. }
  270. }
  271. }
  272. } while (FindNextFile(handle, &fileinfo));
  273. FindClose(handle);
  274. }
  275. }