找回密码
 立即注册
首页 资源区 代码 静态库封装之ComDir类

静态库封装之ComDir类

氛疵 2025-6-4 19:57:17
ComDir.h
  1. /*
  2. @author:EricsT
  3. @data:20241031
  4. @version:V1.0
  5. @history:
  6.         @author @data @version @content
  7.         EricsT 20241031 V1.0 新增ComDir类[判断存在性以及创建目录]
  8.         EricsT 20241101 V1.1 修改isDirExists函数,新增getFilesFromDir函数
  9. */
  10. #pragma once
  11. #include <string>
  12. #include <fstream>
  13. #include <vector>
  14. using namespace std;
  15. class ComDir
  16. {
  17. public:
  18.         /*
  19.         func:判断文件夹是否存在
  20.         @strDirPath:文件夹路径
  21.         @csDirPath:文件夹路径
  22.         @pchDirPath:文件夹路径
  23.         ret:存在true,不存在false
  24.         */
  25.         static bool isDirExists(string strDirPath);
  26.         static bool isDirExists(CString csDirPath);
  27.         static bool isDirExists(PCHAR pchDirPath);
  28.         /*
  29.         func:创建文件夹
  30.         @strDirPath:文件夹路径
  31.         @csDirPath:文件夹路径
  32.         @pchDirPath:文件夹路径
  33.         ret:创建成功true,创建失败false
  34.         */
  35.         static bool creatDir(string strDirPath);
  36.         static bool creatDir(CString csDirPath);
  37.         static bool creatDir(PCHAR pchDirPath);
  38.         /*
  39.         func:获取文件夹[包括子文件夹]内的文件
  40.         @strDirPath/csDirPath/pchDirPath:文件夹路径
  41.         @strExtension/csExtension/pchExtension:扩展名
  42.         ret:vector<string>文件容器
  43.         */
  44.         static vector<string> getFilesFromDir(string strDirPath, string strExtension = "*");
  45.         static vector<string> getFilesFromDir(CString csDirPath, CString csExtension = CString("*"));
  46.         static vector<string> getFilesFromDir(PCHAR pchDirPath, PCHAR pchExtension = "*");
  47. };
复制代码
ComDir.cpp
  1. /*
  2. @author:EricsT
  3. @data:20241031
  4. @version:V1.1
  5. */
  6. #include "stdafx.h"
  7. #include "ComDir.h"
  8. #include <io.h>
  9. #include <direct.h>
  10. bool ComDir::isDirExists(string strDirPath)
  11. {
  12.         struct _finddata_t fileInfo;
  13.         if (-1 == _findfirst(strDirPath.c_str(), &fileInfo))//取文件信息,不存在则为-1
  14.                 return false;
  15.         if (fileInfo.attrib & _A_SUBDIR)
  16.                 return true;
  17.         return false;
  18. }
  19. bool ComDir::isDirExists(CString csDirPath)
  20. {
  21.         //CStringToString
  22.         int len = csDirPath.GetLength();
  23.         PCHAR pch = new char[len + 1];
  24.         size_t pchSize = wcstombs(pch, csDirPath, len + 1);
  25.         if (pchSize == wstring::npos)
  26.         {
  27.                 delete pch;
  28.                 return "";
  29.         }
  30.         string strDirPath(pch);
  31.         delete pch;
  32.         struct _finddata_t fileInfo;
  33.         if (-1 == _findfirst(strDirPath.c_str(), &fileInfo))
  34.                 return false;
  35.         if (fileInfo.attrib & _A_SUBDIR)
  36.                 return true;
  37.         return false;
  38. }
  39. bool ComDir::isDirExists(PCHAR pchDirPath)
  40. {
  41.          struct _finddata_t fileInfo;
  42.          if (-1 == _findfirst(pchDirPath, &fileInfo))//取文件信息,不存在则为-1
  43.                  return false;
  44.          if (fileInfo.attrib & _A_SUBDIR)
  45.                  return true;
  46.          return false;
  47. }
  48. bool ComDir::creatDir(string strDirPath)
  49. {
  50.         string strCreate = strDirPath;
  51.         string strCoplete = strDirPath.substr(0, 2);//取系统盘
  52.         strCreate = strCreate.substr(3);//除去系统盘之后的路径
  53.         while (true)
  54.         {
  55.                 size_t iPos = strCreate.find('\\');
  56.                 strCoplete += '\\' + strCreate.substr(0, iPos);//按层级取目录
  57.                 if (-1 == _access(strCoplete.c_str(), 0)) //判断该目录是否存在
  58.                 {
  59.                         if (0 != _mkdir(strCoplete.c_str()))//不存在就创建目录
  60.                                 return false;
  61.                 }
  62.                 if (strCreate.npos == iPos)//最后一级目录
  63.                         break;
  64.                 strCreate = strCreate.substr(iPos + 1);
  65.         }
  66.         return true;
  67. }
  68. bool ComDir::creatDir(CString csDirPath)
  69. {
  70.         //CStringToString
  71.         int len = csDirPath.GetLength();
  72.         PCHAR pch = new char[len + 1];
  73.         size_t pchSize = wcstombs(pch, csDirPath, len + 1);
  74.         if (pchSize == wstring::npos)
  75.         {
  76.                 delete pch;
  77.                 return "";
  78.         }
  79.         string strDirPath(pch);
  80.         delete pch;
  81.         string strCreate = strDirPath;
  82.         string strCoplete = strDirPath.substr(0, 2);
  83.         strCreate = strCreate.substr(3);
  84.         while (true)
  85.         {
  86.                 size_t iPos = strCreate.find('\\');
  87.                 strCoplete += '\\' + strCreate.substr(0, iPos);
  88.                 if (-1 == _access(strCoplete.c_str(), 0))
  89.                 {
  90.                         if (0 != _mkdir(strCoplete.c_str()))
  91.                                 return false;
  92.                 }
  93.                 if (strCreate.npos == iPos)
  94.                         break;
  95.                 strCreate = strCreate.substr(iPos + 1);
  96.         }
  97.         return true;
  98. }
  99. bool ComDir::creatDir(PCHAR pchDirPath)
  100. {
  101.         string strDirPath(pchDirPath);
  102.         string strCreate = strDirPath;
  103.         string strCoplete = strDirPath.substr(0, 2);
  104.         strCreate = strCreate.substr(3);
  105.         while (true)
  106.         {
  107.                 size_t iPos = strCreate.find('\\');
  108.                 strCoplete += '\\' + strCreate.substr(0, iPos);
  109.                 if (-1 == _access(strCoplete.c_str(), 0))
  110.                 {
  111.                         if (0 != _mkdir(strCoplete.c_str()))
  112.                                 return false;
  113.                 }
  114.                 if (strCreate.npos == iPos)
  115.                         break;
  116.                 strCreate = strCreate.substr(iPos + 1);
  117.         }
  118.         return true;
  119. }
  120. std::vector<std::string> ComDir::getFilesFromDir(string strDirPath, string strExtension /*= "*"*/)
  121. {
  122.         vector<string> strRetVec;
  123.         struct _finddata_t fileInfo;
  124.         long hFile = 0;
  125.         if (-1 == (hFile = _findfirst((strDirPath + "\\*.*").c_str(), &fileInfo)))//判断路径是否存在
  126.                 return strRetVec;
  127.         do
  128.         {
  129.                 if (fileInfo.attrib & _A_SUBDIR)//判断是否是目录
  130.                 {
  131.                         if (("." == string(fileInfo.name)) || (".." == string(fileInfo.name)))
  132.                                 continue;
  133.                        
  134.                         vector<string> vec = getFilesFromDir(strDirPath + "\" + fileInfo.name, strExtension);//递归获取子目录文件
  135.                         strRetVec.insert(strRetVec.end(), vec.begin(), vec.end());//将子容器合并到母容器内
  136.                         continue;
  137.                 }
  138.                 if ("*" == strExtension)//无指定扩展名
  139.                 {
  140.                         strRetVec.push_back(fileInfo.name);
  141.                         continue;
  142.                 }
  143.                 string strName = fileInfo.name;
  144.                 size_t iPos = strName.find_last_of('.');
  145.                 if (strName.npos == iPos)
  146.                 {
  147.                         if ("" == strExtension)
  148.                                 strRetVec.push_back(fileInfo.name);//目标文件
  149.                         continue;
  150.                 }
  151.                 if (strExtension != strName.substr(iPos + 1))//和指定扩展名不一致
  152.                         continue;
  153.                 strRetVec.push_back(fileInfo.name);//目标文件
  154.         } while (0 == _findnext(hFile, &fileInfo));
  155.         return strRetVec;
  156. }
  157. std::vector<std::string> ComDir::getFilesFromDir(CString csDirPath, CString csExtension /*= CString("*")*/)
  158. {
  159.         vector<string> strRetVec;
  160.         //CStringToString
  161.         int len = csDirPath.GetLength();
  162.         PCHAR pch = new char[len + 1];
  163.         size_t pchSize = wcstombs(pch, csDirPath, len + 1);
  164.        
  165.         if (pchSize == wstring::npos)
  166.         {
  167.                 delete pch;
  168.                 return strRetVec;
  169.         }
  170.         string strDirPath(pch);
  171.         delete pch;
  172.         //CStringToString
  173.         int len_1 = csExtension.GetLength();
  174.         PCHAR pch_1 = new char[len_1 + 1];
  175.         size_t pchSize_1 = wcstombs(pch_1, csExtension, len_1 + 1);
  176.         if (pchSize_1 == wstring::npos)
  177.         {
  178.                 delete pch_1;
  179.                 return strRetVec;
  180.         }
  181.         string strExtension(pch_1);
  182.         delete pch_1;
  183.         struct _finddata_t fileInfo;
  184.         long hFile = 0;
  185.         if (-1 == (hFile = _findfirst((strDirPath + "\\*.*").c_str(), &fileInfo)))
  186.                 return strRetVec;
  187.         do
  188.         {
  189.                 if (fileInfo.attrib & _A_SUBDIR)
  190.                 {
  191.                         if (("." == string(fileInfo.name)) || (".." == string(fileInfo.name)))
  192.                                 continue;
  193.                         CString csPath = csDirPath + CString("\") + CString(fileInfo.name);
  194.                         vector<string> vec = getFilesFromDir(csPath, csExtension);
  195.                         strRetVec.insert(strRetVec.end(), vec.begin(), vec.end());
  196.                         continue;
  197.                 }
  198.                 if ("*" == strExtension)
  199.                 {
  200.                         strRetVec.push_back(fileInfo.name);
  201.                         continue;
  202.                 }
  203.                 string strName = fileInfo.name;
  204.                 size_t iPos = strName.find_last_of('.');
  205.                 if (strName.npos == iPos)
  206.                 {
  207.                         if ("" == strExtension)
  208.                                 strRetVec.push_back(fileInfo.name);
  209.                         continue;
  210.                 }
  211.                 if (strExtension != strName.substr(iPos + 1))
  212.                         continue;
  213.                 strRetVec.push_back(fileInfo.name);
  214.         } while (0 == _findnext(hFile, &fileInfo));
  215.         return strRetVec;
  216. }
  217. std::vector<std::string> ComDir::getFilesFromDir(PCHAR pchDirPath, PCHAR pchExtension /*= "*"*/)
  218. {
  219.         vector<string> strRetVec;
  220.         struct _finddata_t fileInfo;
  221.         long hFile = 0;
  222.         if (-1 == (hFile = _findfirst((string(pchDirPath) + "\\*.*").c_str(), &fileInfo)))
  223.                 return strRetVec;
  224.         do
  225.         {
  226.                 if (fileInfo.attrib & _A_SUBDIR)
  227.                 {
  228.                         if (("." == string(fileInfo.name)) || (".." == string(fileInfo.name)))
  229.                                 continue;
  230.                         string strPath = string(pchDirPath) + "\" + string(fileInfo.name);
  231.                         vector<string> vec = getFilesFromDir(strPath.c_str(), pchExtension);
  232.                         strRetVec.insert(strRetVec.end(), vec.begin(), vec.end());
  233.                         continue;
  234.                 }
  235.                 if ("*" == string(pchExtension))
  236.                 {
  237.                         strRetVec.push_back(fileInfo.name);
  238.                         continue;
  239.                 }
  240.                 string strName = fileInfo.name;
  241.                 size_t iPos = strName.find_last_of('.');
  242.                 if (strName.npos == iPos)
  243.                 {
  244.                         if ("" == string(pchExtension))
  245.                                 strRetVec.push_back(fileInfo.name);
  246.                         continue;
  247.                 }
  248.                 if (string(pchExtension) != strName.substr(iPos + 1))
  249.                         continue;
  250.                 strRetVec.push_back(fileInfo.name);
  251.         } while (0 == _findnext(hFile, &fileInfo));
  252.         return strRetVec;
  253. }
复制代码
在VS编译器内会报C4996错误,解决见下文:
C4996 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. - EricsT - 博客园 (cnblogs.com)

来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
您需要登录后才可以回帖 登录 | 立即注册