遍历文件夹以及子目录文件

来源:互联网 发布:淘宝众筹定金和尾款 编辑:程序博客网 时间:2024/06/05 02:29

bool IsRoot(const CString& strPath)
{
 CString Root;
 Root = strPath.GetAt(0)+"://";
 if(Root == strPath)
  return true;
 else
  return false;
}

 

void FindAllFiles(const CString& strPath)
{
 CString szFind;
 szFind = strPath;
 if(!IsRoot(szFind))
  szFind += "//";
 szFind += "*.*";
 WIN32_FIND_DATA FindFileData;
 HANDLE hFind = FindFirstFile(szFind, &FindFileData);
 if(hFind == INVALID_HANDLE_VALUE)
  return ;

 bool bFlag = true;
 do
 {
  if(FindFileData.cFileName[0] == '.') //过滤本级目录和父目录
   continue;
  if(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) //如果找到的是目录,则进入此目录进行递归
  {
   CString szFile;
   if(IsRoot(strPath))
    szFile = strPath + FindFileData.cFileName;
   else
    szFile = strPath + "//" + FindFileData.cFileName;
   FindAllFiles(szFile);
  }
  else //找到的是文件
  {
   CString szFile;
   if(IsRoot(strPath))
    szFile = strPath+FindFileData.cFileName;
   else
   {
    szFile=strPath+"//"+FindFileData.cFileName;  //处理文件

   }
  }
 }
 while(FindNextFile(hFind, &FindFileData) && bFlag);
 FindClose(hFind);
}

 

 

 

原创粉丝点击