遍历文件夹及其子文件夹下所有文件

void dfsFolder(string folderPath, ofstream &fout)
{
    _finddata_t FileInfo;
    string strfind = folderPath + "\\*";
    long Handle = _findfirst(strfind.c_str(), &FileInfo);
    
    if (Handle == -1L)
    {
        cerr << "can not match the folder path" << endl;
        exit(-1);
    }
    do{
        //判断是否有子目录
        if (FileInfo.attrib & _A_SUBDIR)    
        {
            //这个语句很重要
            if( (strcmp(FileInfo.name,".") != 0 ) &&(strcmp(FileInfo.name,"..") != 0))   
            {
                string newPath = folderPath + "\\" + FileInfo.name;
                dfsFolder(newPath, fout);
            }
        }
        else  
        {
            fout << folderPath << "\\" << FileInfo.name  << " ";
        }
    }while (_findnext(Handle, &FileInfo) == 0);

    _findclose(Handle);
    fout.close();
}
void FindFile(wchar_t *pFilePath)
 {
     WIN32_FIND_DATA FindFileData;
     HANDLE hFind = INVALID_HANDLE_VALUE;
     wchar_t  DirSpec[MAX_PATH + 1];// 指定路径 
     DWORD dwError;
     wcsncpy (DirSpec, pFilePath, wcslen(pFilePath) + 1);
     wcsncat (DirSpec, L"\\\*", 3);
     hFind=FindFirstFile(DirSpec,&FindFileData);
     if (hFind == INVALID_HANDLE_VALUE) {
         wprintf(L"Invalid file handle. Error is %u ", GetLastError());
         return ;
     } 
     bool bFinish=false;
     while(!bFinish)
     {
         if (FindFileData.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY )
         {
 
             wchar_t temp[3000];
             memset(temp,0,3000*sizeof(wchar_t));
             //wprintf_s(temp,L"%S\\%S\n",pFilePath,FindFileData.cFileName);
             wcscpy(temp,pFilePath);
             wcscat(temp,L"\\");
             wcscat(temp,FindFileData.cFileName);
             string rawtext="";
             string line;
             ifstream infile;
             infile.open(temp);
             if(infile)
                {
                    while(getline(infile,line))
                    {
                        rawtext+=line;
                    }
                     
 
                }
 
             infile.clear();
             infile.close();
            InsertArticlesToDataBase(rawtext);
 
 
         } 
         bFinish = (FindNextFile(hFind, &FindFileData) == false);
 
     }
 
      
 }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值