How to find subfolders from a root folder

22 02 2010

 

 

How to recurses all subdirectories in a root directory and list out the name of these directories.

 

 

We can use CFileFind MFC class to find the files from a folder and call a member function IsDirectory of this class to determine if the found file is a directory.

 

 

bool FindSubFolders()
{
   CString rootFolderPath = _T("D:\\SAN");
   std::vector& subFolderList;
   CFileFind   finder;
   bool        returnValue = false;
   char        fileName[_MAX_PATH];

   strcpy(fileName, rootFolderPath);
   strcat(fileName, _T("\\*.*"));

   BOOL bOk = finder.FindFile(fileName);

   while (bOk)
   {
      bOk = finder.FindNextFile();

      /** skip . and .. files */
      if (finder.IsDots())
      {
         continue;
      }

      /** Add to list if its a directory */
      if (finder.IsDirectory())
      {
         subFolderList.push_back(finder.GetFilePath());
         returnValue = true;
      }
   }

   finder.Close();

   return returnValue;
}

 

 

A file that is a directory is marked with FILE_ATTRIBUTE_DIRECTORY a file attribute identified in the WIN32_FIND_DATA structure.

Advertisements

Actions

Information

3 responses

21 04 2011
Bee

With your code only list a sub folder (not all sub folder) !!!

Ex:
D:\
A
1
2
B
3
4
C
5
6
You only can get folder (A,B,C) from root folder (D:\).

You can not get all sub folder.

If you can get all sub folder, please help me by sending email to me !!!

Thank you in advance !!!

23 04 2011
Sanoop S P

Hello,

Thanks for your comment.
If you need to get the list of all the sub-folders too, you can recursively call the same function with a small change… please see the code snippet below.

std::vector m_subFolderList;

bool FindSubFolders(CString p_strFolderPath)
{
CFileFind finder;
bool returnValue = false;
char fileName[_MAX_PATH];

strcpy(fileName, p_strFolderPath);
strcat(fileName, _T(“\\*.*”));

BOOL bOk = finder.FindFile(fileName);

while (bOk)
{
bOk = finder.FindNextFile();

/** skip . and .. files */
if (finder.IsDots())
{
continue;
}

/** Add to list if its a directory */
if (finder.IsDirectory())
{
m_subFolderList.push_back(finder.GetFilePath());
returnValue = true;
}

FindSubFolders( finder.GetFilePath() );
}

finder.Close();

return returnValue;
}

void FindAllSunFolders()
{
FindSubFolders (“F:\\RootFolder”);

std::vector::iterator itr = m_subFolderList.begin();
std::vector::iterator itrEnd = m_subFolderList.end();

while (itr != itrEnd)
{
AfxMessageBox(*itr);
++itr;
}
}

Thanks
Sanoop

19 06 2013
mukesh

hello sir
i need help i need to find no of folders and files in each folder so give me written programme i am working in visual stdio 2008 c++ ..

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




%d bloggers like this: