65.9K
CodeProject 正在变化。 阅读更多。
Home

使用 C/C++ 浏览文件夹对话框、搜索文件夹及其所有子文件夹

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.36/5 (18投票s)

2002 年 7 月 20 日

2分钟阅读

viewsIcon

236503

downloadIcon

3680

打开一个浏览文件夹窗口,然后搜索所选文件夹及其所有子文件夹中的每个文件

引言

没有提供截图,也没有编译好的产品。原因是下面的代码是一个用于补充现有代码的工具,而且将其粘贴到您的产品中并调用函数名即可使用,因此我认为每个人都能弄明白。唯一需要确保的是,编辑 SearchFolder 中明确标记的部分,以包含找到有效文件时您想要执行的操作。 浏览文件夹对话框代码的某些部分取自 此处

最佳实现方式是将它包含在 Tools.cpp 中,并根据需要调用它。 确保修改 SearchFolder,使其将文件路径发送到您可能需要它的任何地方。

详细说明

这是您需要使用的第一个函数,BrowseFolder。 它打开一个浏览文件夹对话框,然后调用搜索函数一次(或者说,如果选择了文件夹)。

#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

基本上,它获取当前文件夹,并逐个遍历每个文件。 首先它检查 - 这是否是一个文件夹? 如果是,则启动此函数的新的实例来搜索该子文件夹。 如果不是,那么程序员需要决定如何处理该文件,这在变量 path 中设置。

历史

  • 2002 年 7 月 23 日 - 更新了源代码

许可证

本文没有附加明确的许可,但可能在文章文本或下载文件中包含使用条款。 如有疑问,请通过下面的讨论区联系作者。 可以在 此处 找到作者可能使用的许可列表。

© . All rights reserved.