c++搜索目录下的所有文件

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







相关函数的解释


_fullpath(char*abspath,char* relpath,size_t maxsize);

参数解释


absPath

Pointer to a buffer containing the absolute or full path name, or NULL.

relPath

Relative path name.

maxLength

Maximum length of the absolute path name buffer (absPath ). This length is in bytes for _fullpath but in wide characters (wchar_t ) for _wfullpath .

Each of these functions returns a pointer to a buffer containing the absolute path name (absPath ). If there is an error (for example, if the value passed in relPath includes a drive letter that is not valid or cannot be found, or if the length of the created absolute path name (absPath ) is greater than maxLength ), the function returns NULL .

The _fullpath function expands the relative path name in relPath to its fully qualified or absolute path and stores this name in absPath . If absPath is NULL, malloc is used to allocate a buffer of sufficient length to hold the path name. It is the responsibility of the caller to free this buffer. A relative path name specifies a path to another location from the current location (such as the current working directory: "."). An absolute path name is the expansion of a relative path name that states the entire path required to reach the desired location from the root of the file system. Unlike _makepath , _fullpath can be used to obtain the absolute path name for relative paths (relPath ) that include "./" or "../" in their names.

For example, to use C run-time routines, the application must include the header files that contain the declarations for the routines. Each header file include statement references the location of the file in a relative manner (from the application's working directory):

 
 

改变当前的工作目录到指定的目录中

_chdir, _wchdir

Changes the current working directory.

int _chdir( 

const char *dirname

);

int _wchdir(

const wchar_t *dirname

);
Parameters
dirname

Path of new working directory.

These functions return a value of 0 if successful. A return value of –1 indicates failure. If the specified path could not be found, errno is set to ENOENT . If dirname is NULL, the invalid parameter handler is invoked, as described in Parameter Validation . If execution is allowed to continue, errno is set to EINVAL and the function returns -1.

The _chdir function changes the current working directory to the directory specified by dirname . The dirname parameter must refer to an existing directory. This function can change the current working directory on any drive. If a new drive letter is specified in dirname , the default drive letter is changed as well. For example, if A is the default drive letter and /BIN is the current working directory, the following call changes the current working directory for drive C and establishes C as the new default drive:

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值