CFileFind实现遍历文件夹,获得文件夹下子文件个数,亲测通过

void CCount_fileDlg::OnBnClickedOk()
{
         // TODO: 在此添加控件通知处理程序代码
       CString path = "d:\\Program Files";
       int n = ListFolder(path);
       CString number;
       number.Format("file numbers is:%d",n);
       AfxMessageBox(number);
        //CDialogEx::OnOK();
}
int   g_nCounter   =   0;
int CCount_fileDlg::ListFolder(CString   sPath)

       CFileFind   ff; 
       BOOL   bFound; 
       bFound   =   ff.FindFile(sPath   +   "\\*.* "); 
       while(bFound) 
       { 
              bFound   =   ff.FindNextFile(); 
              CString   sFilePath   =   ff.GetFilePath();

              if(ff.IsDirectory()) 
              { 
                     if(!ff.IsDots()) 
                      CCount_fileDlg::ListFolder(sFilePath); 
              } 
              else 
              { 
                     g_nCounter++; 
              } 
       } 
       //ff.Close(); 
       return g_nCounter;
}

你可能感兴趣的:(mfc学习,numbers,path,file)