Solved

Listing files in a folder

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

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

Suggested Solutions

Title # Comments Views Activity
Macro to save highlighted rows as a separate file 5 45
only14 challenge 19 81
Problem to open Excel file 15 179
Problem to App 4 98
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: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

830 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