做一个函数 在磁盘搜索一个文件夹 比如在D盘里想要找一个A文件夹,A文件不知道在哪里,所以一个个搜索,找到的话就赋值给string字段 各位有没有参考的函数 或小例子也可以 在下学了一个月嘿嘿

解决方案 »

  1.   

    查找文件的,修改一下即可HRESULT FindFile(LPCTSTR dir, LPCTSTR filename, LPTSTR resultPath)
    {
    HRESULT hr = E_FAIL;
    DWORD len = (strlen(dir) + 5) * sizeof(TCHAR); LPTSTR tmpDirExpr =  new TCHAR[strlen(dir) + 5];
    memset(tmpDirExpr, 0, len);
    strcat(tmpDirExpr, dir);
    strcat(tmpDirExpr,_T("//*")); CFileFind *pfilefind = new CFileFind(); BOOL isResOK = pfilefind->FindFile((LPCTSTR)tmpDirExpr);
    while(isResOK)
    {
    isResOK = pfilefind->FindNextFile();
    if(!pfilefind->IsDots())
    {
    CString foundPathStr = pfilefind->GetFilePath();
    if(foundPathStr == filename)
    {
    DWORD len = (foundPathStr.GetLength() + 1) * sizeof(TCHAR);
    memcpy(resultPath,foundPathStr.GetBuffer(), len);
    foundPathStr.ReleaseBuffer();
    hr =  S_OK;
    break;
    } if(pfilefind->IsDirectory())
    {
    CString subDir = pfilefind->GetFilePath();
    if(SUCCEEDED(FindFile(subDir, filename, resultPath)))
    {
    hr =  S_OK;
    break;
    }
    //free(newSubDir);
    }
    }
    } pfilefind->Close();

    delete pfilefind;
    delete tmpDirExpr; return hr;
    }
      

  2.   

    MFC 的 CFileFind类 有个 FindFile 和 FindNextFile 函数,它既不会搜索兄弟目录, 也不会搜索子目录的,但是提供了一个判断函数 CFileFind::IsDirectory(), 判断该文件夹下某个文件路径是不是目录,遍历选择目录的指定扩展名文件
    char szPath[MAX_PATH];     //存放选择的目录路径 
        CString str;

        ZeroMemory(szPath, sizeof(szPath));   

        BROWSEINFO bi;   
        bi.hwndOwner = NULL;   
        bi.pidlRoot = NULL;   
        bi.pszDisplayName = szPath;   
        bi.lpszTitle = "请选择需要打包的目录:";   
        bi.ulFlags = 0;   
        bi.lpfn = NULL;   
        bi.lParam = 0;   
        bi.iImage = 0;   
        //弹出选择目录对话框
        LPITEMIDLIST lp = SHBrowseForFolder(&bi);   

    //目录有效
        if(!lp || !SHGetPathFromIDList(lp, szPath))   
    return;
        

    CFileFind   ff; 
    BOOL   bFound; 
    CString sPath=szPath;
    bFound = ff.FindFile(sPath+ "\*.exe ");//找第一个文件 只是找有没有,并不得到第一个文件 
    while(bFound)//如果找到,继续 

    bFound   =   ff.FindNextFile(); //
    CString   sFilePath =   ff.GetFilePath();
    AfxMessageBox(sFilePath);//枚举到的文件名字 


    ff.Close(); 
      

  3.   

    通过CFileFind写个递归查找即可