递归方式搜索文件

int Find_special_File(LPCTSTR pszPath, //查找起始路径 
                      LPCTSTR pszExt, //文件扩展名 
                      BOOL include_subDirectory, //搜索子目录标志 
                      CListBox *pList) //列表
{
  int found_count = 0; //查找计数器 
  int file_fliter_len = CString(pszExt).GetLength(); //文件扩展名长度 
  CString target_file = pszPath; //目标路径 
  if(target_file.GetLength() > 1 && target_file.Right(1) != '\\')
    target_file += '\\'; 
  if(include_subDirectory) //包括子目录 
    target_file += "*.*"; 
  else target_file += pszExt; 
  bool b_find_all_file = false; //查找所有文件指示 
  if(CString(pszExt) == ".*") //扩展名使用通配符
    b_find_all_file = true; 
  //查找文件
  CFileFind find; 
  BOOL ret = find.FindFile(target_file);  
  while(ret)
  {
    ret = find.FindNextFile(); 
    if(find.IsDots()) //. 或 .. 文件
    { 
    } 
    else if(find.IsDirectory()) //子目录 
    { 
      if(include_subDirectory) //搜索子目录标志 
      {
        //递归方式搜索下一级目录 
        CString next_path = find.GetFilePath(); 
        found_count += Find_special_File(next_path,pszExt,include_subDirectory,pList); } 
    } 
    else //file
    { 
      CString file_Name = find.GetFilePath(); 
      if(b_find_all_file //所有文件
        || ( file_Name.GetLength() > file_fliter_len && file_Name.Right(file_fliter_len) == pszExt) //扩展名匹配
        || 0)
      {
        pList->AddString(file_Name); //插入列表 
        found_count++; //搜索计数增加 
      }
    }
  }
  find.Close(); //关闭搜索

  return(found_count); //返回搜索到数目
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值