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>
#include <shlobj.h>
void BrowseFolder( void )
{
TCHAR path[MAX_PATH];
BROWSEINFO bi = { 0 };
bi.lpszTitle = ("All Folders Automatically Recursed.");
LPITEMIDLIST pidl = SHBrowseForFolder ( &bi );
if ( pidl != 0 )
{
SHGetPathFromIDList ( pidl, path );
SetCurrentDirectory ( path );
SearchFolder( path );
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.
void SearchFolder( TCHAR * path )
{
WIN32_FIND_DATA FindFileData;
HANDLE hFind;
TCHAR filename[ MAX_PATH + 256 ];
TCHAR pathbak[ MAX_PATH ];
strcpy( pathbak, path );
hFind = FindFirstFile ( "*.*", &FindFileData );
do
{
if ( hFind != INVALID_HANDLE_VALUE )
{
if ( ! ( strcmp( FindFileData.cFileName, "." ) ) ||
! ( strcmp( FindFileData.cFileName, ".." ) ) )
{
continue;
}
strcpy( path, pathbak );
sprintf( path, "%s\\%s", path, FindFileData.cFileName );
if ( ( SetCurrentDirectory( path ) ) )
{
SearchFolder( path );
}
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.
History
23 Jul 2002 - udpated source.