Monday, July 22, 2013

Return the list of files from a directory in MFC

Below is the code snippet that implements a C++ function to return the list of files in a directory in MFC:


#include "StdAfx.h"

void CAppUtilManager::GetFiles(const CString& parent_folder, const CString& filters, CStringArray& files, BOOL bRecursive) const
{
 CFileFind fFind;
 BOOL bWorking=fFind.FindFile(parent_folder+_T("\\")+filters);

 while(bWorking==TRUE)
 {
  bWorking=fFind.FindNextFile();
  if(fFind.IsDots())
  {
   continue;
  }
  if(fFind.IsDirectory())
  {
   if(bRecursive==TRUE)
   {
    GetFiles(fFind.GetFilePath(), filters, files, bRecursive);
   }
  }
  else
  {
   files.Add(fFind.GetFilePath());
  }
 }
 fFind.Close();
}

No comments:

Post a Comment