现在的位置: 首页 > 综合 > 正文

c++ 遍历目录查找文件

2018年02月12日 ⁄ 综合 ⁄ 共 1345字 ⁄ 字号 评论关闭

 ///////////////////////////////////////////////////////////
    // Get current dir of exe
    char szModuleFileName[MAX_PATH];
 //得到exe所在的路径包括exe文件
    ::GetModuleFileName(NULL, szModuleFileName, MAX_PATH);
    char szDrive[256], szDir[MAX_PATH];
 //szDrive exe所在盘符,szDir exe所在目录
    _splitpath(szModuleFileName, szDrive, szDir, NULL, NULL);
 
    /* format dir for working dir */
    char szTempPath[MAX_PATH];
 //得到所要查找文件的目录 szDir exe所在目录 "局站停电及发电统计" exe所在目录的子目录
    sprintf(szTempPath, "%s%s", szDir, "局站停电及发电统计");
    char szSearching[MAX_PATH];
    /**********************************************************/
    /* 为了保存一个变量 */
    _makepath(szSearching, szDrive, szTempPath, NULL, NULL);
    CString  m_csPluginDir;
    m_csPluginDir.Format("%s", szSearching);
 //查找的条件是*.xls文件
    _makepath(szSearching, szDrive, szTempPath, "*", "xls");
 
    /*
    ** Search all .dll files, test and load them.
    **/
    CString csFileName;
    WIN32_FIND_DATA wfd;
    HANDLE hFindFile = ::FindFirstFile(szSearching, &wfd);
 if (hFindFile == INVALID_HANDLE_VALUE) return; // Not any *.dll files
    csFileName.Format("%s", wfd.cFileName);
    while(! csFileName.IsEmpty())
    {
      
  
        /* find next file */
        if (! ::FindNextFile(hFindFile, &wfd) /* &&
            GetLastError() == ERROR_NO_MORE_FILES*/)
        {
            ASSERT(GetLastError() == ERROR_NO_MORE_FILES);
            break;
        }
        else csFileName.Format("%s", wfd.cFileName);
    }
    ::FindClose(hFindFile);

抱歉!评论已关闭.