?
Solved

Retrieving Folders Name

Posted on 2002-04-27
7
Medium Priority
?
467 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
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 400 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

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: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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.
Integration Management Part 2
Suggested Courses

840 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