CFileFind find;
CString Path = lpszPath;
CString lpsz =  Path +L"\\";
Path = Path +L"\\*.*";
BOOL IsFind = find.FindFile(Path);
while(IsFind )
{
IsFind=find.FindNextFile();
//如果是"."则不扫描
if(find.IsDots())
continue;
//是目录,继续扫描此目录
else if(find.IsDirectory())
{
CString strPath = lpszPath;
strPath = strPath + L"\\" + find.GetFileName();
ScanDiskFile(strPath);
}
//文件
else
{
//获得文件的路径
m_strFile = find.GetFileName();
CString extend = m_strFile.Right(m_strFile.GetLength() - m_strFile.ReverseFind('.') - 1);//取得扩展名
if (extend == m_ext_one | extend == m_ext_two)//m_ext_now为你要查找的文件扩展名
{
m_strArray.Add(lpsz + m_strFile); 
}
}
}
find.Close();
m_ext_one、m_ext_two用于指定需要搜索的文件后缀名

解决方案 »

  1.   

    这么添加试下。。void CTestDlg::OnButton1()   
    {
    // 选择文件夹
    CString strFolderPath(_T(""));
    TCHAR szPath[_MAX_PATH]; BROWSEINFO bi;
    bi.hwndOwner = GetSafeHwnd();
    bi.pidlRoot = NULL;
    bi.lpszTitle = _T("选择文件夹");
    bi.pszDisplayName = szPath;
    bi.ulFlags = BIF_RETURNONLYFSDIRS;
    bi.lpfn = NULL;
    bi.lParam = NULL; LPITEMIDLIST pItemIDList = SHBrowseForFolder(&bi); if(pItemIDList)
    {
    if(SHGetPathFromIDList(pItemIDList,szPath))
    {
    strFolderPath = szPath;
    } // 防止内存泄露,要使用IMalloc接口
    IMalloc* pMalloc;
    if( SHGetMalloc(&pMalloc) != NOERROR )
    {
    TRACE(_T("无法取得外壳程序的IMalloc接口\n"));
    } pMalloc->Free(pItemIDList);
    if(pMalloc)
    pMalloc->Release();
    }
    else
    {
    strFolderPath = _T(""); // 文件夹路径为空
    } // 接下来添加相关获取文件夹内各文件路径的代码就可以了。。
    // 试下4L的代码。。
    }
      

  2.   

    我需要把文件都读取出来 并且存储到vector中,还能帮我改改吗,谢谢
      

  3.   

    头文件里添加函数声明。。void ScanDiskFile(const CString& strPath);
    std::vector<CString> m_vStrAllFiles;
    void CTestDlg::OnButton1()   
    {
    // 选择文件夹

    // ...... ScanDiskFile(strFolderPath);
    }void CTestDlg::ScanDiskFile(const CString& strPath)
    {
    CFileFind find;
    CString strTemp = strPath;
    CString strDirectory = strPath + _T("\\") + _T("\\*.*");
    CString strFile; BOOL IsFind = find.FindFile(strDirectory);
    while(IsFind )
    {
    IsFind=find.FindNextFile(); // 如果是"." 则不扫描
    if(find.IsDots())
    continue;
    // 如果是是目录,继续扫描此目录
    else if(find.IsDirectory())
    {
    strFile = find.GetFileName();
    strTemp = strTemp + _T("\\") + strFile;
    ScanDiskFile(strTemp);
    }
    // 文件
    else
    {
    strFile = find.GetFileName(); // 此处也可以根据相应的扩展名来设置

    // ...... m_vStrAllFiles.push_back(strFile);
    }
    } find.Close();
    }
      

  4.   

    谢谢你,还有一个问题,m_vStrAllFiles这个容器在应该在哪里定义?
      

  5.   

    BROWSEINFO bi;
    CString Buffer;//初始化入口参数bi开始
    bi.hwndOwner=NULL;
    bi.pidlRoot=NULL;//初始化制定的root目录很不容易
    bi.pszDisplayName = (LPWSTR)(LPCTSTR)Buffer;//次参数如为NULL则不能现实对话框
    bi.lpszTitle=_T("选择目标文件路径");
    //bi.ulFlags=BIF_EDITBOX;//带编辑框的风格
    bi.lpfn=NULL;
    bi.lParam=0;
    bi.iImage=IDR_MAINFRAME;//初始化入口参数bi结束
    LPITEMIDLIST pIDList=SHBrowseForFolder(&bi);
    if(pIDList)
    {
    SHGetPathFromIDList(pIDList,bi.pszDisplayName);//取得文件夹路径到Buffer
    CString m_cSisDes=Buffer;//将路径保存在一个CString对象里
    SetDlgItemText(IDC_EDIT2, Buffer);
    }
    UpdateData(FALSE);
    IMalloc *imalloc=0;
    if(SUCCEEDED(SHGetMalloc((&imalloc))))
    {
    imalloc->Free(pIDList);
    imalloc->Release();
    }给你浏览文件夹
      

  6.   

    这个最后的路径保存到那个变量里了? 我想把浏览到的文件夹路径传到void CTestDlg::ScanDiskFile(const CString& strPath) 里面然后遍历里面的文件,另外是不是把你的这段代码直接放到:
    void CTestDlg::OnButton1()   
    {
        // 选择文件夹
        
        // ......    ScanDiskFile(strFolderPath);
    }
    放到选择文件夹的部分就可以了? 谢谢你
      

  7.   

    m_vStrAllFiles定义为CTestDlg类的成员变量就行了。。呃。。选择文件夹的那段代码不是在7L么。。- -。。
      

  8.   

    太谢谢各位热心朋友了,我把代码整理了一下,放到这里,如果还有哪位像我一样的初学者需要可以直接拿去用了
    代码功能是:点击按钮 浏览文件夹 读取其中的所有文件到vector容器中:void CTestDlg::OnButton1()   
    {
    CString strFolderPath(_T(""));
        TCHAR szPath[_MAX_PATH];

        BROWSEINFO bi;
        bi.hwndOwner = GetSafeHwnd();
        bi.pidlRoot = NULL;
        bi.lpszTitle = _T("选择文件夹");
        bi.pszDisplayName = szPath;
        bi.ulFlags = BIF_RETURNONLYFSDIRS;
        bi.lpfn = NULL;
        bi.lParam = NULL;

        LPITEMIDLIST pItemIDList = SHBrowseForFolder(&bi);

        if(pItemIDList)
        {
            if(SHGetPathFromIDList(pItemIDList,szPath))
            {
                strFolderPath = szPath;
            }

            // 防止内存泄露,要使用IMalloc接口
            IMalloc* pMalloc;
            if( SHGetMalloc(&pMalloc) != NOERROR )
            {
                TRACE(_T("无法取得外壳程序的IMalloc接口\n"));
            }

            pMalloc->Free(pItemIDList);
            if(pMalloc)
                pMalloc->Release();
        }
        else
        {
            strFolderPath = _T("");    // 文件夹路径为空
        }    ScanDiskFile(strFolderPath);
    }void CTestDlg::ScanDiskFile(const CString& strPath)
    {
        CFileFind find;
        CString strTemp = strPath;
        CString strDirectory = strPath + _T("\\") + _T("\\*.*");
        CString strFile;

    vector<CString> m_vStrAllFiles;   // zm容器    BOOL IsFind = find.FindFile(strDirectory);
        while(IsFind )
        {
            IsFind=find.FindNextFile();        // 如果是"." 则不扫描
            if(find.IsDots())
                continue;
            // 如果是是目录,继续扫描此目录
            else if(find.IsDirectory())
            {
                strFile = find.GetFileName();
                strTemp = strTemp + _T("\\") + strFile;
                ScanDiskFile(strTemp);
            }
            // 文件
            else
            {            
                strFile = find.GetFileName();            // 此处也可以根据相应的扩展名来设置
                
                // ......            m_vStrAllFiles.push_back(strFile);
            }
        }    find.Close();
        
            //以下这三行是我测试用的,你可以把它们删掉

            CString temp;
            temp.Format("i = %d", m_vStrAllFiles.size());
            MessageBox(temp);
    }
      

  9.   

    C++语言: 高亮代码由发芽网提供01 C++语言: 高亮代码由发芽网提供
    02 
    03 void CtestDlg::ScanDiskFile(const CString& strPath)
    04      {
    05          CFileFind find;
    06          CString strTemp = strPath;
    07          CString strDirectory = strPath + _T("\\") + _T("\\*.*");
    08          CString strFile;
    09 
    10         vector<CString> m_vStrAllFiles; // zm容器
    11 
    12         BOOL IsFind = find.FindFile(strDirectory);
    13          while(IsFind )
    14          {
    15              IsFind=find.FindNextFile();
    16 
    17             // 如果是"." 则不扫描
    18              if(find.IsDots())
    19                  continue;
    20              // 如果是是目录,继续扫描此目录
    21              else if(find.IsDirectory())
    22              {
    23                  strFile = find.GetFileName();
    24                  strTemp = strTemp + _T("\\") + strFile;
    25                  ScanDiskFile(strTemp);
    26              }
    27              // 文件
    28              else
    29              {   
    30                 strFile = find.GetFileName();
    31 
    32                 // 此处也可以根据相应的扩展名来设置
    33 
    34                 // ......
    35 
    36                 m_vStrAllFiles.push_back(strFile);
    37              }
    38          }
    39 
    40         find.Close();
    41 
    42     }