?
Solved

Retrieving file names based on extention

Posted on 2003-02-21
5
Medium Priority
?
258 Views
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.
0
Comment
Question by:johnsla
[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 1

Accepted Solution

by:
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)
         return;

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

FindClose(handle);
}      

Usage:
// 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]);
0
 
LVL 2

Assisted Solution

by:sreenu_v78
sreenu_v78 earned 100 total points
ID: 8006569
hai
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.
cheers..
sreenu.
0
 

Author Comment

by:johnsla
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.
0

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