c++ 遍历目录下文件

来源:互联网 发布:清华美院艺术史论 知乎 编辑:程序博客网 时间:2024/05/29 00:32

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