C++遍历目录下文件

转载自:http://blog.csdn.net/abcjennifer/article/details/18147551

function:遍历目录下所有文件,返回文件总数,子文件夹总数(修改一下可以获得全部文件名等)。

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #include "stdlib.h"  
  2. #include "direct.h"  
  3. #include "string.h"  
  4. #include "io.h"  
  5. #include "stdio.h"   
  6. #include "iostream"  
  7. using namespace std;  
  8.   
  9. class CBrowseDir  
  10. {  
  11. protected:  
  12.     //存放初始目录的绝对路径,以'\'结尾  
  13.     char m_szInitDir[_MAX_PATH];  
  14.   
  15. public:  
  16.     //缺省构造器  
  17.     CBrowseDir();  
  18.   
  19.     //设置初始目录为dir,如果返回false,表示目录不可用  
  20.     bool SetInitDir(const char *dir);  
  21.   
  22.     //开始遍历初始目录及其子目录下由filespec指定类型的文件  
  23.     //filespec可以使用通配符 * ?,不能包含路径。  
  24.     //如果返回false,表示遍历过程被用户中止  
  25.     bool BeginBrowse(const char *filespec);  
  26.   
  27. protected:  
  28.     //遍历目录dir下由filespec指定的文件  
  29.     //对于子目录,采用迭代的方法  
  30.     //如果返回false,表示中止遍历文件  
  31.     bool BrowseDir(const char *dir,const char *filespec);  
  32.   
  33.     //函数BrowseDir每找到一个文件,就调用ProcessFile  
  34.     //并把文件名作为参数传递过去  
  35.     //如果返回false,表示中止遍历文件  
  36.     //用户可以覆写该函数,加入自己的处理代码  
  37.     virtual bool ProcessFile(const char *filename);  
  38.   
  39.     //函数BrowseDir每进入一个目录,就调用ProcessDir  
  40.     //并把正在处理的目录名及上一级目录名作为参数传递过去  
  41.     //如果正在处理的是初始目录,则parentdir=NULL  
  42.     //用户可以覆写该函数,加入自己的处理代码  
  43.     //比如用户可以在这里统计子目录的个数  
  44.     virtual void ProcessDir(const char *currentdir,const char *parentdir);  
  45. };  
  46.   
  47. CBrowseDir::CBrowseDir()  
  48. {  
  49.     //用当前目录初始化m_szInitDir  
  50.     getcwd(m_szInitDir,_MAX_PATH);  
  51.   
  52.     //如果目录的最后一个字母不是'\',则在最后加上一个'\'  
  53.     int len=strlen(m_szInitDir);  
  54.     if (m_szInitDir[len-1] != '\\')  
  55.         strcat(m_szInitDir,"\\");  
  56. }  
  57.   
  58. bool CBrowseDir::SetInitDir(const char *dir)  
  59. {  
  60.     //先把dir转换为绝对路径  
  61.     if (_fullpath(m_szInitDir,dir,_MAX_PATH) == NULL)  
  62.         return false;  
  63.   
  64.     //判断目录是否存在  
  65.     if (_chdir(m_szInitDir) != 0)  
  66.         return false;  
  67.   
  68.     //如果目录的最后一个字母不是'\',则在最后加上一个'\'  
  69.     int len=strlen(m_szInitDir);  
  70.     if (m_szInitDir[len-1] != '\\')  
  71.         strcat(m_szInitDir,"\\");  
  72.   
  73.     return true;  
  74. }  
  75.   
  76. bool CBrowseDir::BeginBrowse(const char *filespec)  
  77. {  
  78.     ProcessDir(m_szInitDir,NULL);  
  79.     return BrowseDir(m_szInitDir,filespec);  
  80. }  
  81.   
  82. bool CBrowseDir::BrowseDir(const char *dir,const char *filespec)  
  83. {  
  84.     _chdir(dir);  
  85.   
  86.     //首先查找dir中符合要求的文件  
  87.     long hFile;  
  88.     _finddata_t fileinfo;  
  89.     if ((hFile=_findfirst(filespec,&fileinfo)) != -1)  
  90.     {  
  91.         do  
  92.         {  
  93.             //检查是不是目录  
  94.             //如果不是,则进行处理  
  95.             if (!(fileinfo.attrib & _A_SUBDIR))  
  96.             {  
  97.                 char filename[_MAX_PATH];  
  98.                 strcpy(filename,dir);  
  99.                 strcat(filename,fileinfo.name);  
  100.                 cout << filename << endl;  
  101.                 if (!ProcessFile(filename))  
  102.                     return false;  
  103.             }  
  104.         } while (_findnext(hFile,&fileinfo) == 0);  
  105.         _findclose(hFile);  
  106.     }  
  107.     //查找dir中的子目录  
  108.     //因为在处理dir中的文件时,派生类的ProcessFile有可能改变了  
  109.     //当前目录,因此还要重新设置当前目录为dir。  
  110.     //执行过_findfirst后,可能系统记录下了相关信息,因此改变目录  
  111.     //对_findnext没有影响。  
  112.     _chdir(dir);  
  113.     if ((hFile=_findfirst("*.*",&fileinfo)) != -1)  
  114.     {  
  115.         do  
  116.         {  
  117.             //检查是不是目录  
  118.             //如果是,再检查是不是 . 或 ..   
  119.             //如果不是,进行迭代  
  120.             if ((fileinfo.attrib & _A_SUBDIR))  
  121.             {  
  122.                 if (strcmp(fileinfo.name,".") != 0 && strcmp  
  123.                     (fileinfo.name,"..") != 0)  
  124.                 {  
  125.                     char subdir[_MAX_PATH];  
  126.                     strcpy(subdir,dir);  
  127.                     strcat(subdir,fileinfo.name);  
  128.                     strcat(subdir,"\\");  
  129.                     ProcessDir(subdir,dir);  
  130.                     if (!BrowseDir(subdir,filespec))  
  131.                         return false;  
  132.                 }  
  133.             }  
  134.         } while (_findnext(hFile,&fileinfo) == 0);  
  135.         _findclose(hFile);  
  136.     }  
  137.     return true;  
  138. }  
  139.   
  140. bool CBrowseDir::ProcessFile(const char *filename)  
  141. {  
  142.     return true;  
  143. }  
  144.   
  145. void CBrowseDir::ProcessDir(const char   
  146.     *currentdir,const char *parentdir)  
  147. {  
  148. }  
  149.   
  150. //从CBrowseDir派生出的子类,用来统计目录中的文件及子目录个数  
  151. class CStatDir:public CBrowseDir  
  152. {  
  153. protected:  
  154.     int m_nFileCount;   //保存文件个数  
  155.     int m_nSubdirCount; //保存子目录个数  
  156.   
  157. public:  
  158.     //缺省构造器  
  159.     CStatDir()  
  160.     {  
  161.         //初始化数据成员m_nFileCount和m_nSubdirCount  
  162.         m_nFileCount=m_nSubdirCount=0;  
  163.     }  
  164.   
  165.     //返回文件个数  
  166.     int GetFileCount()  
  167.     {  
  168.         return m_nFileCount;  
  169.     }  
  170.   
  171.     //返回子目录个数  
  172.     int GetSubdirCount()  
  173.     {  
  174.         //因为进入初始目录时,也会调用函数ProcessDir,  
  175.         //所以减1后才是真正的子目录个数。  
  176.         return m_nSubdirCount-1;  
  177.     }  
  178.   
  179. protected:  
  180.     //覆写虚函数ProcessFile,每调用一次,文件个数加1  
  181.     virtual bool ProcessFile(const char *filename)  
  182.     {  
  183.         m_nFileCount++;  
  184.         return CBrowseDir::ProcessFile(filename);  
  185.     }  
  186.   
  187.     //覆写虚函数ProcessDir,每调用一次,子目录个数加1  
  188.     virtual void ProcessDir  
  189.         (const char *currentdir,const char *parentdir)  
  190.     {  
  191.         m_nSubdirCount++;  
  192.         CBrowseDir::ProcessDir(currentdir,parentdir);  
  193.     }  
  194. };  
  195.   
  196. void main()  
  197. {  
  198.     //获取目录名  
  199.     char buf[256];  
  200.     printf("请输入要统计的目录名:");  
  201.     gets(buf);  
  202.   
  203.     //构造类对象  
  204.     CStatDir statdir;  
  205.   
  206.     //设置要遍历的目录  
  207.     if (!statdir.SetInitDir(buf))  
  208.     {  
  209.         puts("目录不存在。");  
  210.         return;  
  211.     }  
  212.   
  213.     //开始遍历  
  214.     statdir.BeginBrowse("*.*");  
  215.     printf("文件总数: %d\n子目录总数:%d\n",statdir.GetFileCount(),statdir.GetSubdirCount());  
  216. }   

已在windows上验证有效。

转载自这里


下面我加了BeginBrowseFilenames函数,以vector<char*>形式返回目录中所有文件名。

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1.   
[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #include "stdlib.h"  
  2. #include "direct.h"  
  3. #include "string.h"  
  4. #include "io.h"  
  5. #include "stdio.h"   
  6. #include <vector>  
  7. #include "iostream"  
  8. using namespace std;  
  9.   
  10. class CBrowseDir  
  11. {  
  12. protected:  
  13.     //存放初始目录的绝对路径,以'\'结尾  
  14.     char m_szInitDir[_MAX_PATH];  
  15.   
  16. public:  
  17.     //缺省构造器  
  18.     CBrowseDir();  
  19.   
  20.     //设置初始目录为dir,如果返回false,表示目录不可用  
  21.     bool SetInitDir(const char *dir);  
  22.   
  23.     //开始遍历初始目录及其子目录下由filespec指定类型的文件  
  24.     //filespec可以使用通配符 * ?,不能包含路径。  
  25.     //如果返回false,表示遍历过程被用户中止  
  26.     bool BeginBrowse(const char *filespec);  
  27.     vector<char*> BeginBrowseFilenames(const char *filespec);  
  28.   
  29. protected:  
  30.     //遍历目录dir下由filespec指定的文件  
  31.     //对于子目录,采用迭代的方法  
  32.     //如果返回false,表示中止遍历文件  
  33.     bool BrowseDir(const char *dir,const char *filespec);  
  34.     vector<char*> GetDirFilenames(const char *dir,const char *filespec);  
  35.     //函数BrowseDir每找到一个文件,就调用ProcessFile  
  36.     //并把文件名作为参数传递过去  
  37.     //如果返回false,表示中止遍历文件  
  38.     //用户可以覆写该函数,加入自己的处理代码  
  39.     virtual bool ProcessFile(const char *filename);  
  40.   
  41.     //函数BrowseDir每进入一个目录,就调用ProcessDir  
  42.     //并把正在处理的目录名及上一级目录名作为参数传递过去  
  43.     //如果正在处理的是初始目录,则parentdir=NULL  
  44.     //用户可以覆写该函数,加入自己的处理代码  
  45.     //比如用户可以在这里统计子目录的个数  
  46.     virtual void ProcessDir(const char *currentdir,const char *parentdir);  
  47. };  
  48.   
  49. CBrowseDir::CBrowseDir()  
  50. {  
  51.     //用当前目录初始化m_szInitDir  
  52.     getcwd(m_szInitDir,_MAX_PATH);  
  53.   
  54.     //如果目录的最后一个字母不是'\',则在最后加上一个'\'  
  55.     int len=strlen(m_szInitDir);  
  56.     if (m_szInitDir[len-1] != '\\')  
  57.         strcat(m_szInitDir,"\\");  
  58. }  
  59.   
  60. bool CBrowseDir::SetInitDir(const char *dir)  
  61. {  
  62.     //先把dir转换为绝对路径  
  63.     if (_fullpath(m_szInitDir,dir,_MAX_PATH) == NULL)  
  64.         return false;  
  65.   
  66.     //判断目录是否存在  
  67.     if (_chdir(m_szInitDir) != 0)  
  68.         return false;  
  69.   
  70.     //如果目录的最后一个字母不是'\',则在最后加上一个'\'  
  71.     int len=strlen(m_szInitDir);  
  72.     if (m_szInitDir[len-1] != '\\')  
  73.         strcat(m_szInitDir,"\\");  
  74.   
  75.     return true;  
  76. }  
  77.   
  78. vector<char*> CBrowseDir::BeginBrowseFilenames(const char *filespec)  
  79. {  
  80.     ProcessDir(m_szInitDir,NULL);  
  81.     return GetDirFilenames(m_szInitDir,filespec);  
  82. }  
  83.   
  84. bool CBrowseDir::BeginBrowse(const char *filespec)  
  85. {  
  86.     ProcessDir(m_szInitDir,NULL);  
  87.     return BrowseDir(m_szInitDir,filespec);  
  88. }  
  89.   
  90. bool CBrowseDir::BrowseDir(const char *dir,const char *filespec)  
  91. {  
  92.     _chdir(dir);  
  93.   
  94.     //首先查找dir中符合要求的文件  
  95.     long hFile;  
  96.     _finddata_t fileinfo;  
  97.     if ((hFile=_findfirst(filespec,&fileinfo)) != -1)  
  98.     {  
  99.         do  
  100.         {  
  101.             //检查是不是目录  
  102.             //如果不是,则进行处理  
  103.             if (!(fileinfo.attrib & _A_SUBDIR))  
  104.             {  
  105.                 char filename[_MAX_PATH];  
  106.                 strcpy(filename,dir);  
  107.                 strcat(filename,fileinfo.name);  
  108.                 cout << filename << endl;  
  109.                 if (!ProcessFile(filename))  
  110.                     return false;  
  111.             }  
  112.         } while (_findnext(hFile,&fileinfo) == 0);  
  113.         _findclose(hFile);  
  114.     }  
  115.     //查找dir中的子目录  
  116.     //因为在处理dir中的文件时,派生类的ProcessFile有可能改变了  
  117.     //当前目录,因此还要重新设置当前目录为dir。  
  118.     //执行过_findfirst后,可能系统记录下了相关信息,因此改变目录  
  119.     //对_findnext没有影响。  
  120.     _chdir(dir);  
  121.     if ((hFile=_findfirst("*.*",&fileinfo)) != -1)  
  122.     {  
  123.         do  
  124.         {  
  125.             //检查是不是目录  
  126.             //如果是,再检查是不是 . 或 ..   
  127.             //如果不是,进行迭代  
  128.             if ((fileinfo.attrib & _A_SUBDIR))  
  129.             {  
  130.                 if (strcmp(fileinfo.name,".") != 0 && strcmp  
  131.                     (fileinfo.name,"..") != 0)  
  132.                 {  
  133.                     char subdir[_MAX_PATH];  
  134.                     strcpy(subdir,dir);  
  135.                     strcat(subdir,fileinfo.name);  
  136.                     strcat(subdir,"\\");  
  137.                     ProcessDir(subdir,dir);  
  138.                     if (!BrowseDir(subdir,filespec))  
  139.                         return false;  
  140.                 }  
  141.             }  
  142.         } while (_findnext(hFile,&fileinfo) == 0);  
  143.         _findclose(hFile);  
  144.     }  
  145.     return true;  
  146. }  
  147.   
  148. vector<char*> CBrowseDir::GetDirFilenames(const char *dir,const char *filespec)  
  149. {  
  150.     _chdir(dir);  
  151.     vector<char*>filename_vector;  
  152.     filename_vector.clear();  
  153.   
  154.     //首先查找dir中符合要求的文件  
  155.     long hFile;  
  156.     _finddata_t fileinfo;  
  157.     if ((hFile=_findfirst(filespec,&fileinfo)) != -1)  
  158.     {  
  159.         do  
  160.         {  
  161.             //检查是不是目录  
  162.             //如果不是,则进行处理  
  163.             if (!(fileinfo.attrib & _A_SUBDIR))  
  164.             {  
  165.                 char filename[_MAX_PATH];  
  166.                 strcpy(filename,dir);  
  167.                 strcat(filename,fileinfo.name);  
  168.                 filename_vector.push_back(filename);  
  169.             }  
  170.         } while (_findnext(hFile,&fileinfo) == 0);  
  171.         _findclose(hFile);  
  172.     }  
  173.     //查找dir中的子目录  
  174.     //因为在处理dir中的文件时,派生类的ProcessFile有可能改变了  
  175.     //当前目录,因此还要重新设置当前目录为dir。  
  176.     //执行过_findfirst后,可能系统记录下了相关信息,因此改变目录  
  177.     //对_findnext没有影响。  
  178.     _chdir(dir);  
  179.     if ((hFile=_findfirst("*.*",&fileinfo)) != -1)  
  180.     {  
  181.         do  
  182.         {  
  183.             //检查是不是目录  
  184.             //如果是,再检查是不是 . 或 ..   
  185.             //如果不是,进行迭代  
  186.             if ((fileinfo.attrib & _A_SUBDIR))  
  187.             {  
  188.                 if (strcmp(fileinfo.name,".") != 0 && strcmp  
  189.                     (fileinfo.name,"..") != 0)  
  190.                 {  
  191.                     char subdir[_MAX_PATH];  
  192.                     strcpy(subdir,dir);  
  193.                     strcat(subdir,fileinfo.name);  
  194.                     strcat(subdir,"\\");  
  195.                     ProcessDir(subdir,dir);  
  196.                     return GetDirFilenames(subdir,filespec);  
  197.                 }  
  198.             }  
  199.         } while (_findnext(hFile,&fileinfo) == 0);  
  200.         _findclose(hFile);  
  201.     }  
  202.     return filename_vector;  
  203. }  
  204.   
  205. bool CBrowseDir::ProcessFile(const char *filename)  
  206. {  
  207.     return true;  
  208. }  
  209.   
  210. void CBrowseDir::ProcessDir(const char   
  211.     *currentdir,const char *parentdir)  
  212. {  
  213. }  
  214.   
  215. //从CBrowseDir派生出的子类,用来统计目录中的文件及子目录个数  
  216. class CStatDir:public CBrowseDir  
  217. {  
  218. protected:  
  219.     int m_nFileCount;   //保存文件个数  
  220.     int m_nSubdirCount; //保存子目录个数  
  221.   
  222. public:  
  223.     //缺省构造器  
  224.     CStatDir()  
  225.     {  
  226.         //初始化数据成员m_nFileCount和m_nSubdirCount  
  227.         m_nFileCount=m_nSubdirCount=0;  
  228.     }  
  229.   
  230.     //返回文件个数  
  231.     int GetFileCount()  
  232.     {  
  233.         return m_nFileCount;  
  234.     }  
  235.   
  236.     //返回子目录个数  
  237.     int GetSubdirCount()  
  238.     {  
  239.         //因为进入初始目录时,也会调用函数ProcessDir,  
  240.         //所以减1后才是真正的子目录个数。  
  241.         return m_nSubdirCount-1;  
  242.     }  
  243.   
  244. protected:  
  245.     //覆写虚函数ProcessFile,每调用一次,文件个数加1  
  246.     virtual bool ProcessFile(const char *filename)  
  247.     {  
  248.         m_nFileCount++;  
  249.         return CBrowseDir::ProcessFile(filename);  
  250.     }  
  251.   
  252.     //覆写虚函数ProcessDir,每调用一次,子目录个数加1  
  253.     virtual void ProcessDir  
  254.         (const char *currentdir,const char *parentdir)  
  255.     {  
  256.         m_nSubdirCount++;  
  257.         CBrowseDir::ProcessDir(currentdir,parentdir);  
  258.     }  
  259. };  
  260.   
  261. void main()  
  262. {  
  263.     //获取目录名  
  264.     char buf[256];  
  265.     printf("请输入要统计的目录名:");  
  266.     gets(buf);  
  267.   
  268.     //构造类对象  
  269.     CStatDir statdir;  
  270.   
  271.     //设置要遍历的目录  
  272.     if (!statdir.SetInitDir(buf))  
  273.     {  
  274.         puts("目录不存在。");  
  275.         return;  
  276.     }  
  277.   
  278.     //开始遍历  
  279.   
  280.     vector<char*>file_vec = statdir.BeginBrowseFilenames("*.*");  
  281.     printf("文件总数: %d\n",file_vec.size());  
  282. }   


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值