VC++打开文件夹选择对话框及递归查找指定文件

1.打开文件夹(目录)选择对话框

  Win32 API方式

// 目录选择对话框选择目录
CString GetDirector()
{
    TCHAR szPath[MAX_PATH] = { '\0' };            // 存放选择的目录路径 

    BROWSEINFO bi;
    ZeroMemory(&bi, sizeof(BROWSEINFO));
    bi.hwndOwner = NULL;                         // 父窗口句柄           
    bi.pidlRoot = NULL;                        // 设置开始搜索位置,为NULL默认从the desktop folder开始
    bi.pszDisplayName = szPath;                 // 被选中的文件夹缓冲区地址
    bi.lpszTitle = _T("请选择目录:");             // 该浏览文件夹对话框对话框的显示文本,用来提示该浏览文件夹对话框的功能、作用和目的。
    bi.ulFlags = BIF_RETURNONLYFSDIRS;           // BIF_RETURNONLYFSDIRS表示只显示和返回目录,BIF_BROWSEINCLUDEFILES:显示和返回目录或文件
    bi.lpfn = NULL;                              // 应用程序定义的浏览对话框回调函数的地址
    bi.lParam = 0;                               // 对话框传递给回调函数的一个参数指针
    bi.iImage = 0;                               // 与选中目录相关的图像。该图像将被指定为系统图像列表中的索引值

    // 弹出选择目录对话框
    LPITEMIDLIST lp = SHBrowseForFolder(&bi);   // SHBrowseForFolder用来显示一个让用户可以选择文件夹的对话框,返回值是指向选定的文件夹相对应于所选择根目录地址的路径标识符指针。 
    if (lp && SHGetPathFromIDList(lp, szPath))
    {

        return CString(szPath);
    }
    else
    {
        return _T("");
    }
}

   MFC的方式:

// 选择文件夹
CString folder;
CFolderPickerDialog pickerDialog;
if (pickerDialog.DoModal() == IDOK)
{
    folder = pickerDialog.GetFolderPath();
    TRACE(folder); // Eg: D:\dir
}
else
{
    TRACE(_T("dialog canceled"));
}

 

2.递归查找一个目录下的指定后缀文件

 

// 递归查找目录下指定类型文件
void FindSpecificFiles(const CString& strDir/*eg: "D:\dir"*/, 
    CString strFindFileSuffix/*eg:"exe" or ".exe" */, 
    CStringArray& arrFindFiles/*find result*/)
{
    CString myDataPath, fdPath;            //设置路径变量
    myDataPath = strDir + _T("\\*.*");     //文件夹路径
    CString strTmpSuffix;                  //后缀名变量
    strFindFileSuffix.MakeLower();         //统一用小写或大写

    CFileFind find;
    BOOL bf = find.FindFile(myDataPath);            // Call this member function to open a file search.
    while (bf) {
        bf = find.FindNextFile();                    // Call this member function to continue a file search from a previous call to FindFile.

        // 排除隐藏目录. ..
        if (!find.IsDots()) {
            fdPath = find.GetFilePath();            // Gets the whole path of the found file.

            if (find.IsDirectory()) {
                //如果是文件夹,递归继续往下找                        
                FindSpecificFiles(fdPath, strFindFileSuffix, arrFindFiles);
            }
            else {
                TRACE(_T("%s\n"), fdPath);
                // 所有文件
                if (strFindFileSuffix == _T("*") || strFindFileSuffix == _T(".*") || strFindFileSuffix == _T("*.*")) {
                    arrFindFiles.Add(fdPath);
                }
                // 指定类型文件
                else {
                    // 取后缀名
                    int index = fdPath.ReverseFind('.');
                    strTmpSuffix = fdPath.Right(fdPath.GetLength() - index); // eg:".exe"
                    strTmpSuffix.MakeLower();

                    if (strFindFileSuffix.Find('.') < 0) {
                        strFindFileSuffix = _T(".") + strFindFileSuffix;
                        // 比较
                        if (strTmpSuffix == strFindFileSuffix) {
                            arrFindFiles.Add(fdPath);
                        }
                    }
                    else {
                        // 比较
                        if (strTmpSuffix == strFindFileSuffix) {
                            arrFindFiles.Add(fdPath);
                        }
                    }
                }
            }
        }
    }
    find.Close();
}

// 处理消息
void ProcessEvent()
{
    MSG msg; 
    while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) //获取消息并把该消息从消息队列中移除(防止重复响应)。
    {
        DispatchMessage(&msg); //将消息移交给过程函数
        TranslateMessage(&msg);//翻译消息在合适的机会产生char消息
    }
}

 

3.使用方法:

// 获取目录下所有文件
CStringArray arrFiles;
FindSpecificFiles(_T("D:\Test\Dir"), _T("*"), arrFiles);

 

原文链接: https://www.cnblogs.com/djh5520/p/12985417.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍;

也有高质量的技术群,里面有嵌入式、搜广推等BAT大佬

    VC++打开文件夹选择对话框及递归查找指定文件

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/351546

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年3月2日 上午6:55
下一篇 2023年3月2日 上午6:55

相关推荐