Solved

Retrieving Folders Name

Posted on 2002-04-27
7
454 Views
Last Modified: 2013-11-20
Hi,

I'm trying to get a list of all folders/sub-folders name.

I have tried to use CFileFind::GetFileName to return the folder name but it gives me only the string ".".
I have also tried to use WIN32_FIND_DATA and FindFirstFile to return the folder name but it only works on the first series of folders in c: drive.

So what should i do to get all the folders name within c: drive?

I have read another similar question in experts-exchange submitted by electrick but I still did not get the solution.

Thank You
xstar
0
Comment
Question by:xstar
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 

Expert Comment

by:jerinjohn
ID: 6973044
Hi,
  if u want all the folder names,u get one name and add to the array of strings.In below shows how to get all the folder names in c drive.It find one by one and add to the carray.

CString szDirpath="C:\"; // c drive    
CFileFind strFinder;
CArray<CString,CString> arrayList;

BOOL bFindFile = strFinder.FindFile(szDirpath);  
if(TRUE==bFindFile)          // If file Found
{
while (bFindFile)// Repeat the loop until all files read
{    
   bFindFile = strFinder.FindNextFile();          
  if ( (strFinder.IsDirectory())
   {
    arrayList.Add(strFinder.GetFileName());

   }
}
           
0
 
LVL 12

Accepted Solution

by:
migel earned 100 total points
ID: 6973223
Hi!
if you want get entire dir tree then you must use recursion for this code
for example
CString szDirpath="C:\"; // c drive    
CArray<CString,CString> arrayList;

EnumDirs(szDirpath, arrayList);

void EnumDirs(CString& szDirpath, CArray<CString,CString>& arrayList)
{
CFileFind strFinder;
CString strMask = szDirpath;
strMask += "*.*';
BOOL bFindFile = strFinder.FindFile(strMask);
if(TRUE==bFindFile)          // If file Found
{
while (bFindFile)// Repeat the loop until all files read
{    
  bFindFile = strFinder.FindNextFile();        
 if ( (strFinder.IsDirectory())
  {
   arrayList.Add(strFinder.GetFileName());
// and enumerate all subfolders
  strMask = szDirpath;
  strMask+= strFinder.GetFileName();
  strMask += "\\";
  EnumDirs(strMask, arrayList);
  }
}
0
 

Author Comment

by:xstar
ID: 6975931
Hi,

The 'GetFileName' & 'GetFilePath' function will only return "." if it is a folder. So what should i do to get the actual folder name?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Expert Comment

by:jerinjohn
ID: 6976243
hi,

 U try to display the filenames returned in a messagebox. if it returnes dot then continue.
ie; add the below lines within the loop

     if(strFinder.IsDots())
        continue;

hope it will helps u

Jerry
0
 

Author Comment

by:xstar
ID: 6979102
sorry, didn't quite get it. so how do i get the folder name instead of the "."? Using "isDots" function can only determine whether it is a folder, it does not tells me the name of the folder.

Thank you
xstar
0
 

Expert Comment

by:1cand01t
ID: 6979713
i ve done a similar thing in the following code, i ve to populate a tree ctrl (commented out).

first directory is always "." which denotes the current directory and the second ".." denotes the parent directory, u can exclude them by IsDots() function.

this may help u.

///////////////////////////
CFileFind finder;
BOOL bWorking = finder.FindFile(Path + "\\*.*");
while (bWorking)
{
bWorking = finder.FindNextFile();
if ((finder.IsDots())||(finder.IsHidden())||(finder.IsSystem()))
continue;
if (finder.IsDirectory())
{
CString strFilename = (LPCTSTR) finder.GetFileName();
//LeftTreeCtrl.InsertItem(TVIF_CHILDREN | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_TEXT,strFilename, 1, 2, 0, 0, 0, hPA, TVI_ROOT);
}
if (!finder.IsDirectory())
{
CString strFilename = (LPCTSTR) finder.GetFileName();
//lvItem.mask=LVIF_TEXT | LVIF_IMAGE;
//lvItem.iItem=0;
//lvItem.iSubItem=0;
//lvItem.pszText = strFilename.GetBuffer(sizeof(strFilename));
//lvItem.iImage=0;
//nItem = ListCtrl.InsertItem(&lvItem);
}
}
////////////////////////
0
 

Author Comment

by:xstar
ID: 6979984
sorry to all, i think i make a very stupid mistake.
at first i thought that the return "." is the file name for the subfolders but i just found out that the GetFileName will only return "." if the directory is a parent or current directory. therefore, the GetFileName actually works.

I awarded the points to migel because he provided a recursive funtion to add all the folders/subfolders to the list.

Thank you all for the help given and sorry for the mistake that i made.

Regards
xstar
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question