• MFC dfs遍历文件


    //如果涉及到大文件的遍历(大于4GB),可以将以下代码_finddata_t换成__finddata64_t,_findfirst换成_findfirst64,_findnext换成_findnext64

    void dfsFolder(CString dirPath)
    {
     _finddata_t FileInfo;
     CString tmp=dirPath;
     if (tmp.Right(1) != "\")
      tmp += "\";
     CString strfind = tmp + "*";
     long Handle = _findfirst(strfind, &FileInfo);
     if (Handle == -1L)
     {
      //cerr << "can not match the folder path" << endl;
      return ;
     }
     do{
      //判断是否有子目录
      if (FileInfo.attrib&_A_SUBDIR)
      {
       // 由于系统在进入一个子目录时,匹配到的头两个文件(夹)
       // 是"."(当前目录),".."(上一层目录)。需要忽略掉这两种情况
       if( (strcmp(FileInfo.name,".") != 0 ) &&(strcmp(FileInfo.name,"..") != 0))  
       {
        //判断是否处理隐藏文件夹、系统文件夹
        //if ((!(FileInfo.attrib&_A_HIDDEN)||PROC_HIDDEN)&&
        // (!(FileInfo.attrib&_A_SYSTEM)||PROC_SYSTEM))
        {
         CString newPath = dirPath + "\" + FileInfo.name;
         //递归遍历更深层次的文件夹
         dfsFolder(newPath);
        }
       }
      }
      else 
      {

        CString fileSizeStr;
        //文件大小
        if (FileInfo.size/1024.0<1)
        {
         fileSizeStr.Format("%I64dB",FileInfo.size);
        }
        else if (FileInfo.size/(1024.0*1024.0)<1)
        {
         fileSizeStr.Format("%.2I64fKB",FileInfo.size/1024.0);
        }

      //最后修改文件的时间
        CTime time(FileInfo.time_write);
        CString timeStr = time.Format( "%Y/%m/%d %H:%M:%S" );
       // 文件名字(包括后缀名)
       CString nameExt(FileInfo.name);
       int i=nameExt.ReverseFind('.');
       CString name=nameExt.Left(i);
       CString ext=nameExt.Mid(i+1);
      }
     }while (_findnext(Handle, &FileInfo) == 0);
     _findclose(Handle);
    }

    ---------------------------------------------------------------------------------

    //以下代码可以复制带有子文件夹的文件夹

    void dfsCopyFolder(CString srcPath, CString dstPath)
    {
     _finddata_t FileInfo;
     CString tmp=srcPath;
     if (tmp.Right(1) != "\")
      tmp += "\";
     CString strfind = tmp + "*";
     long Handle = _findfirst(strfind, &FileInfo);
     if (Handle == -1L)
     {
      //cerr << "can not match the folder path" << endl;
      return ;
     }
     do{
      //判断是否有子目录
      if (FileInfo.attrib&_A_SUBDIR)
      {
       // 由于系统在进入一个子目录时,匹配到的头两个文件(夹)
       // 是"."(当前目录),".."(上一层目录)。需要忽略掉这两种情况
       if( (strcmp(FileInfo.name,".") != 0 ) &&(strcmp(FileInfo.name,"..") != 0))  
       {
        //判断是否处理隐藏文件夹、系统文件夹
        //if ((!(FileInfo.attrib&_A_HIDDEN)||PROC_HIDDEN)&&
        // (!(FileInfo.attrib&_A_SYSTEM)||PROC_SYSTEM))
        {
         CString newSrcPath = srcPath + "\" + FileInfo.name;
         CString newDstPath = dstPath + "\" + FileInfo.name;
         if (!PathFileExists(newDstPath))//判断是否存在重名文件
         {
          //生成目标文件夹
          if(!CreateDirectory(newDstPath,NULL))  
          {
           //AfxMessageBox("创建文件夹失败!");
          }
         }
         //递归遍历更深层次的文件夹
         dfsCopyFolder(newSrcPath, newDstPath);
        }
       }
      }
      else 
      {
       // 文件名字(包括后缀名)
       CString nameExt(FileInfo.name);
       int i=nameExt.ReverseFind('.');
       CString name=nameExt.Left(i);
       CString ext=nameExt.Mid(i+1);
       CString srcFile=srcPath+"\"+nameExt;
       CString dstFile=dstPath+"\"+nameExt;
       BOOL b=CopyFile(srcFile, dstFile, false);//强行覆盖
       if (!b)
       {
        //AfxMessageBox("复制"+srcFile+"文件失败!");
       }

      }
     }while (_findnext(Handle, &FileInfo) == 0);
     _findclose(Handle);
    }

  • 相关阅读:
    【Cookie】java.lang.IllegalArgumentException An invalid character [32] was present in the Cookie value
    【会话技术】Cookie技术 案例:访问时间
    进程池
    管道和Manager模块(进程之间的共享内容)
    队列
    锁Lock,信号量Semaphore,事件机制Event
    multiprocess模块
    进程
    reduce
    struct模块
  • 原文地址:https://www.cnblogs.com/coolbear/p/3458118.html
Copyright © 2020-2023  润新知