文件遍历函数

来源:互联网 发布:github上的源码怎么用 编辑:程序博客网 时间:2024/04/29 22:09

 void   BrowseDir(CString   strDir)     //此函数用于遍历文件夹下的文件,strDir是一个目录路径  
  {  
        if(strDir   ==   "")  
        {  
              MessageBox("error!");  
              return;  
        }  
        CFileFind   ff;  
        CString   szDir   =   strDir,strPath;  
        if(szDir.Right(1)   !=   "//")       //保证目录是以/结尾的  
              szDir   +=   "//";  
        szDir   +=   "*.*";  
        BOOL   res   =   ff.FindFile(szDir);  
        while(   res   )  
              {  
                    res   =   ff.FindNextFile();  
  strPath   =   ff.GetFilePath();  
  if(ff.IsDirectory()   &&   !ff.IsDots())  
        BrowseDir(strPath);       //如果目标是个文件夹,则利用嵌套来遍历  
  else   if(!ff.IsDirectory()   &&   !ff.IsDots())  
        DoSth(strPath);       //如果目标是个文件,则对它进行处理  
              }  
        ff.Close();  
  }

 

 

 


void   ListFolder(CString   sPath){  
  CFileFind   ff;  
                    BOOL   bFound   =   ff.FindFile(sPath   +   "//*.*");  
  while(bFound){ bFound   =   ff.FindNextFile();  
  CString   sFilePath   =   ff.GetFilePath();  
  if(ff.IsDirectory()){  
  if(!ff.IsDots())  
  ListFolder(sFilePath);  
  } else ListFile(sFilePath);//这里记录的就是所有的文件  
  }  
  ff.Close();  
  }

 

 


step   1:    
  得到目录下所有文件存入m_strArrFilename.  
   
  //xxx.h  
  CStringArray   m_strArrFilename;  
   
  //xxx.cpp  
  void   CTest6Dlg::OnButton1()    
  {    
                FindMyFile("D://1//");  
                CString   str="";  
  }  
  void   CTest6Dlg::FindMyFile(CString   path)  
  {  
          BOOL   bFind,   bFindSuffix;  
  CFileFind   tempFind,   tempFind1;  
   
  _chdir(path);  
  bFind   =   tempFind.FindFile("*.*");  
   
  while(bFind)  
  {  
  bFind   =   tempFind.FindNextFile();  
  if(tempFind.IsDirectory())  
  {  
  if   (!tempFind.IsDots()   )  
  {  
  CString   temppath;  
  temppath   =   tempFind.GetFilePath();  
  FindMyFile(temppath);    
  }  
  }  
  }  
   
  _chdir(path);  
  bFindSuffix   =   tempFind1.FindFile("*.*");  
   
  while(bFindSuffix)  
  {  
  bFindSuffix   =   tempFind1.FindNextFile();  
  CString   filepath,   filename;  
  if(   !tempFind1.IsDirectory()   &&   !tempFind1.IsDots())  
  {  
  filepath   =   tempFind1.GetFilePath();  
  m_strArrFilename.Add(filepath);  
  }  
  }  
  tempFind.Close();  
  tempFind1.Close();  
  }  
   
   
  -----------------------------------------------  
  step   2:  
  得到文件的一些属性,并比较时间  
   
  WIN32_FIND_DATA   ffd;  
  HANDLE   hFind   =   FindFirstFile(m_strArrFilename[0],   &ffd);    
  FILETIME   ftCreationTime   =   ffd.ftCreationTime;  
  FILETIME   ftLastWriteTime   =   ffd.ftLastWriteTime;  
   
  然后比较所有文件的时间,进行排序。。。