Link to home
Start Free TrialLog in
Avatar of xstar
xstar

asked on

Retrieving Folders Name

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
Avatar of jerinjohn
jerinjohn

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());

   }
}
           
ASKER CERTIFIED SOLUTION
Avatar of migel
migel

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of xstar

ASKER

Hi,

The 'GetFileName' & 'GetFilePath' function will only return "." if it is a folder. So what should i do to get the actual folder name?
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
Avatar of xstar

ASKER

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
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);
}
}
////////////////////////
Avatar of xstar

ASKER

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