C++下 遍历文件夹

来源:互联网 发布:淘宝四件套好评语50字 编辑:程序博客网 时间:2024/05/16 19:08

CFileFind 实现

 

CFileFind ff;
 CString l_strFilePath;
 CString l_strFileName;
 CString l_strFileExt;
 CString l_strFileTitle;
 CString l_strFileUrl;
 CString l_strFileRoot;
 DWORD l_dwLength;
 CString l_strResult;

 int l_nPoint;
 int l_nLength;

 CString  m_strResult = "";

CString  m_strDir = "f://hoho";
 

 CString l_strDir = m_strDir;

if(l_strDir.Right(1) != "//")
  l_strDir += "//"; 
 l_strDir += "*.*"; 
 BOOL res = ff.FindFile(l_strDir);
 while(res)
 {
  res = ff.FindNextFile();
  // 不遍历子目录
  if(!ff.IsDirectory() && !ff.IsDots())
  {
   l_strFilePath = ff.GetFilePath();
   l_strFileName = ff.GetFileName();
   
   l_nPoint = l_strFileName.ReverseFind('.'); // 因为文件名中可能出现多个'.'所以用ReverseFind而不用Find?
   l_nLength = l_strFileName.GetLength();   
   l_strFileExt = l_strFileName.Right(l_nLength - l_nPoint - 1);
   
   l_strFileTitle = ff.GetFileTitle();
   l_strFileUrl = ff.GetFileURL();
   l_strFileRoot = ff.GetRoot();
   l_dwLength = ff.GetLength();
   l_strResult.Format("Filepath: %s/r/nFileName: %s/r/nFileExe: %s/r/nnFileTitle: %s/r/nFileUrl: %s/r/nFileRoot: %s/r/nFileLength: %d/r/n/r/n", l_strFilePath, l_strFileName, l_strFileExt, l_strFileTitle, l_strFileUrl, l_strFileRoot, l_dwLength);
   m_strResult += l_strResult;  
  }
 }
 ff.Close();