Solved

Retrieving Folders Name

Posted on 2002-04-27
7
447 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
Technology Partners: 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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
dog bark java program 15 121
Creating an alias to a directory so I can change the directory 'contents' by changing the alias 5 95
matchUp  challenge 9 125
Work with App store 7 94
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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 a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

749 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