Search
 
SCRIPT & CODE EXAMPLE
 

CPP

Browse Folder Dialog, Search Folder and All Sub Folders using C/C++

#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


//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
Comment

PREVIOUS NEXT
Code Example
Cpp :: convert string to wide string 
Cpp :: find the mminimum of the vector and its position in c++ 
Cpp :: generate random ints and floats 
Cpp :: check if number is positive or negative in cpp 
Cpp :: c++ read_ascii 
Cpp :: operazioni aritmetiche c++ 
Cpp :: result += a +b in c++ meaning 
Cpp :: copy constructor in c++ questions 
Cpp :: partition in STL using vector 
Cpp :: c++ FAILED: objekt aufruf : symbol(s) not found for architecture x86_64 
Cpp :: cf 633b trivial problem explanation 
Cpp :: how to delete repeated element in stack c++ 
Cpp :: number triangle c++ 
Cpp :: auto keyword 
Cpp :: how to run c++ on cmd 
Cpp :: In-range Adder 
Cpp :: c++ switch integer 
Cpp :: hwo to send token on redirection in passport 
Cpp :: android call custom managed method from native code 
Cpp :: how to get the last digit of a number 
Cpp :: how to check code execution time in visual studio c++ 
Cpp :: nlohmann json, writing to json file 
Cpp :: lower bound c++ 
Cpp :: combination sum iv leetcode 
Cpp :: c++ cin string 
Cpp :: equal elements in two arrays in c++ 
Cpp :: c++ error 0xC0000005 
C :: c colourful text 
C :: factorial in c 
C :: convert string to float c 
ADD CONTENT
Topic
Content
Source link
Name
2+5 =