Solved

Listing files in a folder

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Looking for a specific application/software 2 91
move a line in eclipse 3 60
lucky13 challenge 11 112
MaxSpan challenge 9 67
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…
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.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

705 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now