CFILEFIND的排序——递归与循环遍历目录中函数的剥离

由于CFILEFIND在查找文件的时候返回的顺序不能确定,所以我们要对CFILEFIND的结果放入容器进行排序。

 

以下是CFILEFIND函数的说明

The FindNextFile function searches for files by name only; it cannot be used for attribute-based searches. It searches both the long and short file names.
In rare cases, file attribute information on NTFS file systems may not be current at the time you call this function. To be assured of getting the current NTFS file system file attributes, call GetFileInformationByHandle.
The order in which this function returns the file names is dependent on the file system type. With the NTFS file system and CDFS file systems, the names are returned in alphabetical order. With FAT file systems, the names are returned in the order the files were written to the disk, which may or may not be in alphabetical order.

 

FindNextfile函数只用文件名来查找文件,它用于基于属性的查找。它查找或长或短的文件名。

在NTFS文件系统,少数情况下文件属性信息可能会在你调用此函数的时候并不是最新的。为了保证获得最新的NTFS文件系统下的文件属性,调用GetFileInformationByHandle函数。

此函数返回文件名的顺序与文件系统类型有关。在NTFS文件系统与CDFS文件系统下,文件名以字母顺序返回。在FAT文件系统下,文件名以写入磁盘的先后次序返回,写入磁盘的先后次序可能与字母次序相同。

 

容器定义如下:

 //第一个字段为要处理的文件,第二个字段为路径

//需要根据第一个字段排序

typedef vector< pair<CString, CString> >  vTwaSort;

 

//第一个字段为目录名,第二个字段为需要排序的需要处理文件与文件路径的pair的数组

//即目录与目录下的需处理文件的映射,需要记录目录结构
typedef pair<CString ,vTwaSort> rootToTwaSort;

 

类中成员

//见typedef的注释

rootToTwaSort m_pairRootToTwaSort;


//见typedef的注释

vTwaSort m_vTwaSortXml;

 

//记录路径,待递归完后将最后个rootToTwaSort push back
CString m_strRoot;

 

//目录与需要处理文件pair的映射的数组
vector< rootToTwaSort > m_vRootToTwaSort;

 

 

//在递归中记录需要处理的文件(原来是直接调用函数)

//静态变量记录目录是否改变

     static CString st_str_TwaRoot = tempFind.GetRoot();
     CString str_TwaRoot = tempFind.GetRoot();
     if (str_TwaRoot != st_str_TwaRoot)
     {//目录改变则将上个目录push back(大盒子),并继续将新的目录下的文件push back(小盒子) 

       //注意有2个vector的级别 大盒子与小盒子,以及映射,详见前面成员定义部分
      m_pairRootToTwaSort.first = st_str_TwaRoot;//这里记录前一个遍历的目录
      m_pairRootToTwaSort.second = m_vTwaSortXml;//这里记录前一个遍历目录的twa文件
      m_vRootToTwaSort.push_back(m_pairRootToTwaSort);//保存
      m_vTwaSortXml.clear();//清空,去记录下一个目录的twa文件
      st_str_TwaRoot = str_TwaRoot;//改变static变量

      CString filePath=tempFind.GetFilePath();
      pair<CString,CString> tempPair;
      tempPair.first = strFile;
      tempPair.second = filePath;
      m_vTwaSortXml.push_back(tempPair);
     }
     else
     {//目录不变的情况下将需要处理文件记录
      m_strRoot = str_TwaRoot;
      CString filePath=tempFind.GetFilePath();
      pair<CString,CString> tempPair;
      tempPair.first = strFile;
      tempPair.second = filePath;
      m_vTwaSortXml.push_back(tempPair);//记录当前遍历目录twa文件的fileName与filePath
     }

 

//循环结束之后处理

 

//2011.1.26 对CFILEFIND的结果排序,
  //否则可能出现完全相反的结果
  //A函数与B函数为递归循环中剥离出来的


  //用容器装进去排序之后再执行

  //以下为最后一个目录的映射也将其加入数组
  m_pairRootToTwaSort.first = m_strRoot;
  m_pairRootToTwaSort.second = m_vTwaSortXml;
  m_vRootToTwaSort.push_back(m_pairRootToTwaSort);

  //目录按名字排序
  sort(m_vRootToTwaSort.begin(), m_vRootToTwaSort.end());
  for (vector< rootToTwaSort >::iterator iRootToTwaSort= m_vRootToTwaSort.begin();
   iRootToTwaSort != m_vRootToTwaSort.end();
   iRootToTwaSort++)
  {

   //目录下的文件按名字排序
   sort(iRootToTwaSort->second.begin(), iRootToTwaSort->second.end());
   CString tempRootStr = iRootToTwaSort->first;
   for (vTwaSort::iterator iTwaSort = iRootToTwaSort->second.begin();
    iTwaSort!= iRootToTwaSort->second.end();
    iTwaSort++)
   {//执行的操作,模拟了递归中的执行,并按排序后的顺序操作
    Afunction(iTwaSort->first.GetString(),iTwaSort->second.GetString());

    Bfunction(tempRootStr, iTwaSort->first, widgetRtDir, iTwaSort->first);
   }
  }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值