Click here to Skip to main content
Licence 
First Posted 19 Jul 2002
Views 162,158
Bookmarked 42 times

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

By | 22 Jul 2002 | Article
Opens a Browse Folder window, then searches each file of the selected folder and all subfolders.

Introduction

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 http://www.mvps.org/vcfaq/sdk/20.htm.

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.

Details

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 ( );
        }
    }
}//BROWSE FOLDER

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     
    do     
    {         
        //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, ".." ) ) )             
            {                 
                continue;             
            }             
            //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 );
}//SEARCH FOLDER

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.

History

23 Jul 2002 - udpated source.

License

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

JHawkZZ



United States United States

Member

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.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralThanks a lot PinmemberMikeprod10:31 10 Sep '11  
GeneralMFC has CFileFind can do these Pinmemberzhuhuigong18:34 20 Dec '10  
Generalerror PinmemberInisca0:32 30 Apr '10  
GeneralVery useful PinmemberAntonio Mesa0:34 19 Feb '10  
GeneralThanks.. I just converted your code to the MFC style. PinmemberPCC75116:37 21 Jun '09  
GeneralThnaks for the time saving PinmemberRich Hall15:13 3 Feb '09  
GeneralHelp me PinmemberVan Phuong Nguyen0:23 16 May '07  
Generalsome files appear twice PinmemberSebastian Pipping20:05 31 Mar '05  
GeneralGreat! PinmemberSebastian Pipping17:56 30 Mar '05  
GeneralThanks PinmemberRiceking11:17 31 Jan '05  
GeneralI bet you didn't know... Pinmember.:floyd:.18:06 15 Jan '05  
GeneralCD Rom search Pinsussjamesw200312:21 7 Apr '04  
Generalhelp!!!!!! Pinmember010774120:27 1 Oct '03  
GeneralCool PinmemberHockey12:37 6 Aug '03  
GeneralObsevation PinmemberJohn R. Shaw6:43 2 Aug '03  
GeneralBrowse Pinmemberzsoph8:33 23 Jul '03  
GeneralJust for setting current directory PinmemberMrFox3:13 19 Jul '03  
GeneralRe: Just for setting current directory PinmemberJohn R. Shaw7:04 2 Aug '03  
GeneralRe: Just for setting current directory PinmemberMrFox19:01 2 Aug '03  
General谢谢! Pinmemberlenghost1:09 2 Jan '03  
Generalthx Pinsussfunvill6:56 30 Oct '02  
GeneralThe reason for this is due to the code below being a tool to compliment existing code Pinmemberokigan9:14 24 Jul '02  
GeneralRe: The reason for this is due to the code below being a tool to compliment existing code PinmemberSwinefeaster11:14 8 Nov '03  
Generalif ( pidl != 0 ) bracket missing... PinsussAnonymous18:42 20 Jul '02  
GeneralRe: if ( pidl != 0 ) bracket missing... PinsubeditorNishant S18:23 21 Jul '02  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web01 | 2.5.120517.1 | Last Updated 23 Jul 2002
Article Copyright 2002 by JHawkZZ
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid