Solved

Retrieving Folders Name

Posted on 2002-04-27
7
410 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 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

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: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
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.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

762 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now