?
Solved

Reading directories in MFC

Posted on 2003-02-19
3
Medium Priority
?
780 Views
Last Modified: 2013-11-20
I recently have been working on a c++ client server program, the beta version of the server is complete, and I'm working on the client, but the client i would like to create in MFC for visual effects. One problem i found is that I need to read in the content of a Directory. I found a way in C++ to do this unsing opendir(), readdir() and a few other functions. I wrote a Directory class to make it easier, but now in MFC i can't use any of the opendir() or readdir() functions because they use c++ files that won't work with MFC. the problem occurred in including the files sys/types.h and dirent.h. Is there any way to get this to work, or to read a directory using MFC functions. I would like if possible to do this all in code and not dialog boxes, so lets assume that the folder name is in a char *. any way i can do this?
0
Comment
Question by:necroticworld
[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
  • 2
3 Comments
 
LVL 1

Accepted Solution

by:
jackrabbit22 earned 200 total points
ID: 7984472
Try using FindFirstFile, FindNextFile functions.

The following will place all files in a folder into a CStringArray.

The folderpath passed to the function is assumed to have a trailing slash. i.e. C:\Windows\Desktop\ vs. C:\Windows\Desktop

void ListFolderContents(LPCTSTR lpszFolderPath, CStringArray& fileListing)
{
     WIN32_FIND_DATA findData;

        // Add the wildcard extension
     TCHAR curFolder[MAX_PATH];
     _tcscpy(curFolder, lpszFolderPath);
     _tcscat(curFolder, _T("*.*"));

     HANDLE handle = FindFirstFile(curFolder, &findData);

     if (handle == INVALID_HANDLE_VALUE)
          return;

     // Process all files within current directory
     do
     {
          TRACE(_T("Filename: %s\n"), findData.cFileName);

          // Add file to our array
          fileListing.Add(findData.cFileName);

          // If you want to test if a file is a folder
//         if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)

          // Be sure to check for . and .. folders
     } while (FindNextFile(handle, &findData));
}
0
 
LVL 1

Expert Comment

by:jackrabbit22
ID: 7984479
Forget something...

Add this line after the do/while loop:

FindClose(handle);
0
 

Author Comment

by:necroticworld
ID: 7985558
Thanx that helped alot.
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

Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
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 tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

765 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