Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Listing files in a folder

Posted on 2004-08-14
5
Medium Priority
?
182 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
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
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.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .

824 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