Solved

Listing files in a folder

Posted on 2004-08-14
5
179 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 75 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

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: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
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.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

738 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