Click here to Skip to main content
13,141,838 members (54,694 online)
Click here to Skip to main content
Add your own
alternative version


46 bookmarked
Posted 19 Jul 2002

Browse Folder dialog, search folder and all sub folders using C/C++.

, 22 Jul 2002
Rate this:
Please Sign up or sign in to vote.
Opens a Browse Folder window, then searches each file of the selected folder and all subfolders.


No screenshot is available, nor is a compiled product. The reason for this is due to the code below being a tool to compliment existing code, and since it's as easy as pasting it into your product and calling the function name to use, I felt everyone would be able to figure it out.The only thing to make sure of is to edit the clearly marked section of SearchFolder to include what you want done when it finds a valid file. Some parts of the Browse Folder dialog code taken from

The best way to implement this is to include it in a Tools.cpp and call it as needed. Be sure to modify SearchFolder so that it sends the path file wherever you may need it.


This is the first function you will need, BrowseFolder. This opens a Browse Folder dialog and then calls the search function once ( or if, rather ) a folder is chosen.

#include <windows.h>
#include <string.h>

//This is needed for virtually 
//everything in BrowseFolder.
#include <shlobj.h>   

//BROWSE FOLDER - Opens a browse folder dialog.
void BrowseFolder( void )
    TCHAR path[MAX_PATH];
    BROWSEINFO bi = { 0 };
    bi.lpszTitle = ("All Folders Automatically Recursed.");
    LPITEMIDLIST pidl = SHBrowseForFolder ( &bi );

    if ( pidl != 0 )
        // get the name of the folder and put it in path
        SHGetPathFromIDList ( pidl, path );

        //Set the current directory to path
        SetCurrentDirectory ( path );

        //Begin the search
        SearchFolder( path );

        // free memory used
        IMalloc * imalloc = 0;
        if ( SUCCEEDED( SHGetMalloc ( &imalloc )) )
            imalloc->Free ( pidl );
            imalloc->Release ( );

Okay. Now we know what folder the user wants to access, and need to search that folder and all of its sub-folders. This is the hard part of the code, and took a bit of time to figure out, but is quite fast and efficient.

//SEARCH FOLDER - Searches folder and all sub-folders, 
//reading every file it comes across.
void SearchFolder( TCHAR * path ) 
    //Declare all needed handles     
    WIN32_FIND_DATA FindFileData;     
    HANDLE hFind;     
    TCHAR filename[ MAX_PATH + 256 ];     
    TCHAR pathbak[ MAX_PATH ];     

    //Make a backup of the directory the user chose         
    strcpy( pathbak, path );

    //Find the first file in the directory the user chose     
    hFind = FindFirstFile ( "*.*", &FindFileData );

    //Use a do/while so we process whatever FindFirstFile returned     
        //Is it valid?         
        if ( hFind != INVALID_HANDLE_VALUE )         
            //Is it a . or .. directory? If it is, skip, or we'll go forever.             
            if ( ! ( strcmp( FindFileData.cFileName, "." ) ) || 
                ! ( strcmp( FindFileData.cFileName, ".." ) ) )             
            //Restore the original directory chosen by the user             
            strcpy( path, pathbak );

            //Append the file found on to the path of the 
            //directory the user chose             
            sprintf( path, "%s\\%s", path, FindFileData.cFileName );

            //If SetCurrentDirectory Succeeds ( returns 1 ) the 
            //current file is a directory. Pause this function,             
            //and have it call itself. This will begin the whole 
            //process over in a sub directory.             
            if ( ( SetCurrentDirectory( path ) ) )             
                SearchFolder( path );             

            //Otherwise right here is where you need to 
            //insert what you want to do.             
            //As an example let's add the filename to a list box.             
            //INSERT WHAT YOU WANT DONE BELOW!             
            SendMessage( m_listbox_hwnd, LB_ADDSTRING, 0, path );
    while ( FindNextFile ( hFind, &FindFileData ) 
        && hFind != INVALID_HANDLE_VALUE );     
    FindClose ( hFind );

Basically what this does is takes the current folder, and goes through every file one at a time. First it checks - Is this a folder? If it is, start a new instance of this function searching that sub-folder. If not, then it's up to you the programmer to decide what to do with the file, which is set in the variable path.


23 Jul 2002 - udpated source.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

United States United States
Jered McFerron ( JHawkZZ ) enjoys using computers ( often too much ), playing guitar, video games, and most of all, programming in C/C++.

I've been programming in C/C++ since January 2002 and plan on finishing college with a degree in Software Engineering/Computer Programming. I'm currently 19 and attending a college in Sacramento.

You may also be interested in...


Comments and Discussions

QuestionAccess Violation?? Pin
Member 1060087416-Feb-14 9:44
memberMember 1060087416-Feb-14 9:44 
GeneralThanks a lot Pin
Mikeprod10-Sep-11 10:31
memberMikeprod10-Sep-11 10:31 
GeneralMFC has CFileFind can do these Pin
zhuhuigong20-Dec-10 18:34
memberzhuhuigong20-Dec-10 18:34 
Generalerror Pin
Inisca30-Apr-10 0:32
memberInisca30-Apr-10 0:32 
GeneralVery useful Pin
Antonio Mesa19-Feb-10 0:34
memberAntonio Mesa19-Feb-10 0:34 
GeneralThanks.. I just converted your code to the MFC style. Pin
PCC75121-Jun-09 16:37
memberPCC75121-Jun-09 16:37 
GeneralThnaks for the time saving Pin
Rich Hall3-Feb-09 15:13
memberRich Hall3-Feb-09 15:13 
GeneralHelp me Pin
Van Phuong Nguyen16-May-07 0:23
memberVan Phuong Nguyen16-May-07 0:23 
Generalsome files appear twice Pin
Sebastian Pipping31-Mar-05 20:05
memberSebastian Pipping31-Mar-05 20:05 
GeneralGreat! Pin
Sebastian Pipping30-Mar-05 17:56
memberSebastian Pipping30-Mar-05 17:56 
GeneralThanks Pin
Riceking31-Jan-05 11:17
memberRiceking31-Jan-05 11:17 
GeneralI bet you didn't know... Pin
.:floyd:.15-Jan-05 18:06
member.:floyd:.15-Jan-05 18:06 
GeneralCD Rom search Pin
jamesw20037-Apr-04 12:21
sussjamesw20037-Apr-04 12:21 
Generalhelp!!!!!! Pin
01077411-Oct-03 20:27
member01077411-Oct-03 20:27 
GeneralCool Pin
Hockey6-Aug-03 12:37
memberHockey6-Aug-03 12:37 
GeneralObsevation Pin
John R. Shaw2-Aug-03 6:43
memberJohn R. Shaw2-Aug-03 6:43 
GeneralBrowse Pin
zsoph23-Jul-03 8:33
memberzsoph23-Jul-03 8:33 
GeneralJust for setting current directory Pin
MrFox19-Jul-03 3:13
memberMrFox19-Jul-03 3:13 
GeneralRe: Just for setting current directory Pin
John R. Shaw2-Aug-03 7:04
memberJohn R. Shaw2-Aug-03 7:04 
GeneralRe: Just for setting current directory Pin
MrFox2-Aug-03 19:01
memberMrFox2-Aug-03 19:01 
General&#35874;&#35874;&#65281; Pin
lenghost2-Jan-03 1:09
memberlenghost2-Jan-03 1:09 
Generalthx Pin
funvill30-Oct-02 6:56
sussfunvill30-Oct-02 6:56 
GeneralThe reason for this is due to the code below being a tool to compliment existing code Pin
okigan24-Jul-02 9:14
memberokigan24-Jul-02 9:14 
GeneralRe: The reason for this is due to the code below being a tool to compliment existing code Pin
Swinefeaster8-Nov-03 11:14
memberSwinefeaster8-Nov-03 11:14 
Generalif ( pidl != 0 ) bracket missing... Pin
Anonymous20-Jul-02 18:42
sussAnonymous20-Jul-02 18:42 

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.170915.1 | Last Updated 23 Jul 2002
Article Copyright 2002 by JHawkZZ
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid