Retrieving file names based on extention

Posted on 2003-02-21
Medium Priority
Last Modified: 2013-11-20
Does anyone have a generic C, C++, or MFC routine that I can use to get all the file names in a directory with a certain file extention?  Similar to the way the CFileDialog class gleens out files based on extention.
Question by:johnsla
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

Accepted Solution

jackrabbit22 earned 100 total points
ID: 7996425
Try using FindFirstFile, FindNextFile...

void ListFolderContents(LPCTSTR lpszFolderPath, const CString& strExt, CStringArray& fileListing)
     // lpszFolderPath - path of folder to get files from (i.e. C:\Windows\Desktop\) - you must include trailing slash on the path!
     // strExt - file extension to look for (i.e. *.txt)
     // fileListing - holds the files matching the extension

    WIN32_FIND_DATA findData;

    // Add the wildcard extension
    TCHAR curFolder[MAX_PATH];
    _tcscpy(curFolder, lpszFolderPath);
    _tcscat(curFolder, strExt);

    HANDLE handle = FindFirstFile(curFolder, &findData);

    if (handle == INVALID_HANDLE_VALUE)

    // Process all files within current directory
         // Add file to our array
    } while (FindNextFile(handle, &findData));


// This gets all .txt files on the desktop:

CStringArray fileListing;
ListFolderContents(_T("C:\\Windows\\Desktop\\"), _T("*.txt"), fileListing);

     for (int i = 0; i < fileListing.GetSize(); i++)
          TRACE(_T("Filename: %s\n"), fileListing[i]);

Assisted Solution

sreenu_v78 earned 100 total points
ID: 8006569
use CFileFind like this way

#include <direct.h>//for chdir()
CFileFind finder;
CString strpath;

chdir(strpath); //path of directory
   BOOL bWorking = finder.FindFile("*.exe");//this find the file with extension exe only.
   while (bWorking)
      bWorking = finder.FindNextFile();
      cout << (LPCTSTR) finder.GetFileName() << endl;

featch the all files with extension .exe in the specified directory.

Author Comment

ID: 8010613
I thank you both for your solutions.  Because sreenu v78 was the first to respond I should award him the points but thank you jackrabbit22 as well.

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
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.
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

752 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