?
Solved

Listing files in a folder

Posted on 2004-08-14
5
Medium Priority
?
181 Views
Last Modified: 2013-11-20

How can I get a list of all the files in a given folder?

In VB this can be done usig the Filesystem object, but im not sure how it can be done in MFC.

Thanks.
0
Comment
Question by:V2dB
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 11801105
0
 
LVL 5

Expert Comment

by:Nass89
ID: 11802952
Hi,
The following function adds the folder file list to a ListCtrl :

CString m_csPath;//The path of  folder
WIN32_FIND_DATA FileData;
HANDLE hSearch;
DWORD dwAttrs;
BOOL fFinished = FALSE;

      CString str;
// Start searching for .TXT files in the current directory.
      str.Format("%s\\*.txt",m_csPath);
hSearch = FindFirstFile(str, &FileData);
if (hSearch == INVALID_HANDLE_VALUE)
{
    AfxMessageBox("No files found.");
      return;
}
 
// Copy each .TXT file to the new directory
// and change it to read only, if not already.
 CString cs;
while (!fFinished)
{
      cs.Format("%s\\%s",m_csPath,FileData.cFileName);
      if(PathIsDirectory(cs))
            cs.Format("[%s]",FileData.cFileName);
      else
      cs=FileData.cFileName;

if(FileData.cFileName=="." || FileData.cFileName=="..");
else{
//Here I add it to a list control       
m_listDir.AddString(cs);
}

    if (!FindNextFile(hSearch, &FileData))
    {
        if (GetLastError() == ERROR_NO_MORE_FILES)
        {
//            AfxMessageBox( "No more files.");
            fFinished = TRUE;
        }
        else
        {
            AfxMessageBox("Couldn't find next file.");
        }
    }
}

Good Luck!
0
 
LVL 9

Accepted Solution

by:
Priyesh earned 300 total points
ID: 11803289
Here's one using MFC's CFindFile - Usage - ListFiles("c:") ;

void ListFiles(LPCTSTR lpszDir)
{
      CString szSearchString ;
      szSearchString.Format(_T("%s\\*.*"), lpszDir) ;

      CFileFind oFinder ;
      BOOL bWorking = oFinder.FindFile(szSearchString) ;

      while(bWorking)
      {
            bWorking = oFinder.FindNextFile() ;

            if(oFinder.IsDots())
                  continue ;

            TRACE(_T("%s"), oFinder.GetFileName()) ;

            if(oFinder.IsDirectory())
                  TRACE(_T("\t\t\t Dir")) ;

            TRACE(_T("\n")) ;
      }
}
0
 
LVL 5

Expert Comment

by:Nass89
ID: 11807008
Hi 'V2dB',
Would you please explain, what is the difference between my comment and the accepted answer?

Thanks!
0
 

Author Comment

by:V2dB
ID: 11807184
Nass89,

I was looking for the MFC way to do this.. I looked at all 3 comments, and the CFileFind method was the easiest as it provides some useful methods to get file information such as IsDirectory(), it was also less code, he provided a complete and working function and it was also the MFC way of doing it.

Thank you!
0

Featured Post

Get MongoDB database support online, now!

At Percona’s web store you can order your MongoDB database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card. Handle your MongoDB database support now!

Question has a verified solution.

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

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

801 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