VC下遍历文件夹查找文件

今天看到以前的一些代码,觉得比较常用,放在下面供初学者使用吧。下面的代码都采用VS2002编译测试通过。参数dir是查找的根目录,filename为要查找的文件名(区分大小写), 第三个参数为找到后返回找到的文件路径,传入TCHAR resultPath[MAX_PATH];类型。

 

 

  1. /** 
  2. * 1. 采用Win32 API: FindFirstFile() 和 WIN32_FIND_DATA  
  3. */  
  4. HRESULT AcadDocProcessor::FindFile(LPCTSTR dir, LPCTSTR filename, LPTSTR resultPath)  
  5. {  
  6.     HRESULT hr = E_FAIL;  
  7.     TCHAR tmpDirExpr[MAX_PATH];  
  8.     memset(tmpDirExpr, 0, MAX_PATH * sizeof(TCHAR));  
  9.     strcat(tmpDirExpr, dir);  
  10.     strcat(tmpDirExpr,_T("//*"));  
  11.   
  12.     WIN32_FIND_DATA FindFileData;  
  13.   
  14.     HANDLE hFind=::FindFirstFile(tmpDirExpr,&FindFileData);  
  15.     if(INVALID_HANDLE_VALUE == hFind)  
  16.         return hr;  
  17.   
  18.     while(TRUE)  
  19.     {  
  20.         if(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)  
  21.         {  
  22.             if(FindFileData.cFileName[0]!='.')  
  23.             {  
  24.                 TCHAR szFile[MAX_PATH];  
  25.                 strcpy(szFile, dir);  
  26.                 strcat(szFile, "//");  
  27.                 strcat(szFile, FindFileData.cFileName);  
  28.                 hr = FindFile(szFile, filename, resultPath);  
  29.                 if(SUCCEEDED(hr))  
  30.                     break;  
  31.             }  
  32.         }  
  33.         else  
  34.         {  
  35.             if(strcmp(FindFileData.cFileName, filename) == 0)  
  36.             {  
  37.                 strcpy(resultPath, dir);  
  38.                 strcat(resultPath, "//");  
  39.                 strcat(resultPath, FindFileData.cFileName);  
  40.                 //DWORD len = strlen(FindFileData.cFileName) * sizeof(TCHAR) + 1;  
  41.                 //memcpy(resultPath, FindFileData.cFileName, len);  
  42.                 hr =  S_OK;  
  43.                 break;  
  44.             }  
  45.         }  
  46.   
  47.         if(!FindNextFile(hFind,&FindFileData))      
  48.             break;  
  49.     }  
  50.   
  51.     FindClose(hFind);  
  52.     return hr;  
  53. }  
  54.  

  55. 2.:以下是使用CFindFile类的实现:(注意在delete目录的时候需要调用CFindFile类的close函数否则会导致,句柄没有释放无法删除目录)
  56. 以下是使用CFindFile类的实现
    BOOL SearchFile(CString cstrSourceDir, CString cstrFileName, CString &cstrOutFilePath)
    {
        //判断传入的文件夹路径是否有'/'
        DWORD dwLen = cstrSourceDir.GetLength();
        TCHAR ch = cstrSourceDir.GetAt(dwLen-1);
        if (ch != '\\')
        {
            cstrSourceDir = cstrSourceDir + _T("\\");
        }
        
        //判断文件是否存在
        if ( !PathFileExists(cstrSourceDir))
        {
            return FALSE;
        }
        
        BOOL bResult = FALSE;
        CString cstrFindFile = cstrSourceDir + _T("*.*");

        CFileFind cFileFind;
        BOOL bFind = FALSE;
        bFind = cFileFind.FindFile(cstrFindFile,0);
        while(bFind)
        {
            bFind = cFileFind.FindNextFile();
            if ( !cFileFind.IsDots())
            {

                if (cFileFind.IsDirectory())
                {
                    CString cstrSubPath = cFileFind.GetFilePath();
                    if ( SearchFile(cstrSubPath, cstrFileName, cstrOutFilePath))
                    {
                        bResult = TRUE;
                        break;
                    }
                    
                }
                else
                {
                    CString cstrTmpFileName = cFileFind.GetFileName();
                    if (0 == cstrTmpFileName.CompareNoCase(cstrFileName))
                    {    
                        //查找成功返回输出路径
                        cstrOutFilePath = cFileFind.GetFilePath();
                        bResult = TRUE;
                        break;
                    }
                }

            }
                
        };
    //使用完需要关机文件句柄   
  57. cFindFile.close();
        return bResult;
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值