Click here to Skip to main content
13,195,564 members (67,377 online)
Click here to Skip to main content
Add your own
alternative version


19 bookmarked
Posted 3 Nov 2006

Browse for Multiple Files and Folders

, 3 Nov 2006
Rate this:
Please Sign up or sign in to vote.
An article on a Browse Dialog which helps you to select multilple files and folders.

Sample Image


This code snippet will show you how to select multiple files and folders by adding a custom dialog box to your project. There are built-in controls in MFC to select a folder (SHBrowseForFolder) or a file (CFileDialog), but you cannot select multiple folders or files using these controls. Here, I am giving you a dialog box; you can call it from your application, and can select multiple files and folders at a time.

This article will also give you an idea about:

  • How to edit your resource (.rc and Resource.h) file
  • How to extract the system icon list
  • How to traverse through the folder structure of Windows

How to use the code

Here, you have to do a little job. It is an MFC application and uses a dialog, so you have to manually edit your .rc and Resource.h files, and also your App class. So, let's start to use the code..

  1. Copy the BrowseDlg.h and BrowseDlg.cpp files to your project folder.
  2. Change the project header file of BrowseDlg.cpp from #include "BrowseForFiles.h" to your project header file.
  3. Change the name of the App class that is used in the BrowseDlg.cpp from extern CBrowseForFilesApp theApp; to your App class. Repeat it wherever it occurs.
  4. Open your project header file, and add the following lines as public members:
    CImageList m_ImgList;
    CList<CString,CString&> MainDirList;
  5. Then, open your project .cpp file and add the following lines to InitInstance() before beginning the execution of your code:
    HIMAGELIST  hSystemSmallImageList;
    SHFILEINFO    ssfi;
    //get a handle to the system small icon list
     hSystemSmallImageList =
  6. Then, open your Resource.h file and take care while editing. Add the following lines to the list of #defines:
    #define IDD_BROWSE                      129
    #define IDC_PATH                        1002
    #define IDC_DRIVE                       1003
    #define IDC_FILES                       1004
    #define IDC_UP                          1005

    Here, you must take care to avoid duplication of constants. For example, if 129 or 1003 or any of the above numbers already exists, then you must modify the numbers to a new one that is not allocated by any other controls. Here, the best idea is to find the highest three digit number from your file and give it to IDD_BROWSE. And find the highest four digit number and assign it to IDC_PATH, and assign the numbers to the above controls with an increment of one. For example, if 1023 is the highest number assigned to controls, then modify the above values as follows:

    #define IDC_PATH                        1024
    #define IDC_DRIVE                       1025
    #define IDC_FILES                       1026
    #define IDC_UP                          1027

    So you have added controls to the Resource file. Now, modify the APISTUDIO defined constants also, as follows:

    Give the highest three digit value plus one (+1) to the following variable:

    #define _APS_NEXT_RESOURCE_VALUE        130

    Give the highest four digit value plus one (+1) to the following variable:

    #define _APS_NEXT_CONTROL_VALUE         1028

    These variables are used by APISTUDIO when you add new controls to the project.

  7. Then, open your .rc file in text mode and add the following code to the Dialog section:
    CAPTION "Add Files and Folders"
    FONT 8, "MS Sans Serif"
           DEFPUSHBUTTON   "OK",IDOK,112,188,50,14
               PUSHBUTTON      "Cancel",IDCANCEL,48,187,50,14
               EDITTEXT        IDC_PATH,7,165,200,16,
                               ES_AUTOHSCROLL | ES_READONLY
               COMBOBOX        IDC_DRIVE,76,13,29,125,
                               CBS_DROPDOWN | CBS_SORT | 
                               WS_VSCROLL | WS_TABSTOP
               CONTROL         "List1",IDC_FILES,"SysListView32",
                               LVS_REPORT | WS_BORDER | 
               LTEXT           "Drive",IDC_STATIC,51,16,18,8
               LTEXT           "Path",IDC_STATIC,7,155,16,8
               PUSHBUTTON      "UP",IDC_UP,114,13,20,14

    And also add the following code to the DESIGNINFO section:

         LEFTMARGIN, 7
         TOPMARGIN, 7
         BOTTOMMARGIN, 207

So the BrowseDlg is ready to use. Call the dialog wherever you want as follows:

CBrowseDlg dlgBrowse;

After completing execution of the dialog, you will get the selected list of files in the app variable named MainDirList. You can retrive the contents of the CList as follows:

POSITION    position;
// Your App class
extern        CTestBrwsApp theApp;
int iCnt      = theApp.MainDirList.GetCount();
position  = theApp.MainDirList.GetHeadPosition();

for(i = 0; i < iCnt; i++ )
    //m_SelectedList is simply a CListCtrl 

So we completed the job. If you have any doubts, please refer to the sample project provided with this article.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer
India India
simple and cool

You may also be interested in...


Comments and Discussions

QuestionMultiple Folder Selection in C#? Pin
jsimpkins12-Dec-06 5:05
memberjsimpkins12-Dec-06 5:05 
AnswerRe: Multiple Folder Selection in C#? Pin
vaseef12-Dec-06 17:56
membervaseef12-Dec-06 17:56 
GeneralSloppy Pin
PJ Arends3-Nov-06 10:47
memberPJ Arends3-Nov-06 10:47 
GeneralRe: Sloppy Pin
vaseef3-Nov-06 18:55
membervaseef3-Nov-06 18:55 
GeneralRe: Sloppy Pin
Steven Flores27-Feb-07 13:01
memberSteven Flores27-Feb-07 13:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171019.1 | Last Updated 3 Nov 2006
Article Copyright 2006 by vaseef
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid