• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 190
  • Last Modified:

Getting a directory listing

Is there an MFC way to get a list of files and subdirectories from a directory? If not, is there any good way? It doesn't need to be cross-platform compliant (though this is quite obvious after mentioning that I'm using MFC :P ).
0
SonicX_BlueBlur
Asked:
SonicX_BlueBlur
1 Solution
 
AxterCommented:
Hi SonicX_BlueBlur,
You can use Windows API FindFirstFile function

David Maisonave :-)
Cheers!
0
 
AxterCommented:
Here's an example:

WIN32_FIND_DATA fd;
     HANDLE hFind = ::FindFirstFile (_T ("*.exe"), &fd);
     if (hFind != INVALID_HANDLE_VALUE)
     {
          do
          {
               TRACE(fd.cFileName);
                                       // Do some thing  
          } while (::FindNextFile (hFind, &fd));
          ::FindClose (hFind);
     }
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
SonicX_BlueBlurAuthor Commented:
Using the code that starts with the WIN32_FIND_DATA declaration, is it possible to determine if an entry is a directory or a file?
0
 
rcarlanCommented:
If you're using MFC, you may want to also look at CFileFind:

CFileFind oFileFind;
BOOL bContinue = oFileFind.FindFile(_T("*.txt"));
while (bContinue)
{
    bContinue = oFileFind.FindNextFile();
    if (oFileFind.IsDirectory())
    {
        // do something with the directory
    }
    else
    {
        // do something with the file
    }
}

Radu
0
 
nonubikCommented:
>Using the code that starts with the WIN32_FIND_DATA declaration, is it possible to determine if an entry is a directory or a file?

WIN32_FIND_DATA FindData;

.....

if(FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
  //we have a directory
}
0
 
SonicX_BlueBlurAuthor Commented:
I think rcarlan's way would be more of what I'm looking for. I assume I can also search for *.*?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now