如何得到一个文件夹下包含子目录与文件的数量和

解决方案 »

  1.   


    这个我也会啊,只是有点慢啊,6万个文件的遍历需要1分多钟吧,有没有更快的啊?
    void CDeleteRubbishFileDlg::GetFolderFilesCount(char *pPathName)
    {
    CFileFind finder;
    char pathName[LEN],childPath[LEN];
    BOOL flag;
    CString childDir;
    memset(pathName,0x00,LEN);
    flag=finder.FindFile();
    while(flag)
    {
    flag=finder.FindNextFile();
    if(finder.IsDots())
    {
    continue;
    }
    memset(childPath,0x00,LEN);
    childDir=finder.GetFileName();
    lAllNumber++;
    if(finder.IsDirectory())
    {  
    CString temp = finder.GetFilePath();
    char *pTemp;
    pTemp = (LPSTR)(LPCTSTR)temp;
    ::_chdir(pTemp);
    GetFolderFilesCount(childPath);//递归调用
    }
    else
    {
    }
    }
    }
      

  2.   

    FindFirstFile+FindNextFile+FindClose+递归。
      

  3.   

    jlpdgy004的方法正解,不慢了。
      

  4.   

    那么如何将它做成后台工作呢?我按下按钮的同时需要遍历这个文件夹下的文件,将符合要求的文件写到cListCtrl控件中,由于写入的时间较长,需要有个进度条,怎么做呢?
      

  5.   

    自己递归计算即可,请参考下面的代码,自己加上一个计数器就行了。
    CStringArray m_arrType;
    m_arrType.RemoveAll();
    m_arrType.Add("*.dpj");//可以添加不同的搜索后缀
    //如果sPath为目录,就继续检查他的是否仍然有下一级子目录;
    void CDlgPrjManager::SearchDirectory(LPCTSTR Path)
    {
    CString sPath=Path;
    if(sPath.Right(1)!='\\')
    {
    sPath += "\\";
    }
    FindFiles(sPath);
    sPath+="*.*";
    CFileFind ff;
    BOOL bWorking = ff.FindFile(sPath);
    if (!bWorking) return;

    while (bWorking)
    {
    bWorking = ff.FindNextFile();
    if (ff.IsDots())
    continue;

    if (ff.IsDirectory()){
    CString str=ff.GetFilePath();
    str+='\\';
    SearchDirectory(str);
    }
    }
    }//如果sPath为文件,就自动搜索是否符合后缀要求,符合就添加到列表中; 
    void CDlgPrjManager::FindFiles(CString sPath)
    {
    CString sDestPath="";
    CString sExt;//记录后缀
    int nExtLen = 0;
    CString sFileName = "";
    int size=m_arrType.GetSize();
    for(int i=0;i<size;i++){
    CString TmpPath;
    TmpPath=sPath;
    sExt = m_arrType[i];//加上对文件类型的判断
    TmpPath+=sExt;
    sExt = sExt.Mid(1);//去掉“*”.
    nExtLen = sExt.GetLength();
    CFileFind ff;
    BOOL bWorking = ff.FindFile(TmpPath);
    if (!bWorking && i<size ) continue;
    else if(!bWorking && i>=size) return;

    while (bWorking)
    {
    bWorking = ff.FindNextFile();

    if (ff.IsDots())
    {
    continue;
    }
    if (ff.IsDirectory()){ //this is impossible.
    continue;
    }
    if (!ff.IsDirectory()) //file
    {
    sFileName = ff.GetFilePath();
    //AddNewItemToList(sFileName, false);
    }
    }
    }
    }
      

  6.   

    int m_nDirNum;
    int m_nFileNum;在构造函数中初始化
    m_nDirNum = 0;
    m_nFileNum = 0;void FindMyFile(CString path)
    {
        BOOL bFind, bFindSuffix;
    CFileFind tempFind, tempFind1;

    _chdir(path);
    bFind = tempFind.FindFile("*.*");

    while(bFind)
    {
    bFind = tempFind.FindNextFile();
    CString str = tempFind.GetFilePath();
    if(tempFind.IsDirectory())
    {
    if (!tempFind.IsDots() )
    {
    m_nDirNum++;
    CString temppath;
    temppath = tempFind.GetFilePath();
    FindMyFile(temppath); 
    }
    }
    }

    _chdir(path);
    bFindSuffix = tempFind1.FindFile("*.*");

    while(bFindSuffix)
    {
    bFindSuffix = tempFind1.FindNextFile();
    CString tempfile;
    if( !tempFind1.IsDirectory() && !tempFind1.IsDots())
    {
    tempfile = tempFind1.GetFilePath();
    m_strArrFilename.Add(tempfile);
    m_nFileNum++;
    }
    }
    tempFind.Close();
    tempFind1.Close();
    }
      

  7.   

    快?
    没得快了
    Windows自己算6W多个也要好长时间呢吧
      

  8.   

    我自己一直用的一个好东西,不敢独享,建议楼主看看,应该比你的快http://www.vckbase.com/document/viewdoc/?id=1060