zoukankan      html  css  js  c++  java
  • VC++打开文件夹选择对话框及递归查找指定文件

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

      Win32 API方式

    // 目录选择对话框选择目录
    CString GetDirector()
    {
        TCHAR szPath[MAX_PATH] = { '' };            // 存放选择的目录路径 
    
        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
    "), 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:TestDir"), _T("*"), arrFiles);
  • 相关阅读:
    laravel报错1071 Specified key was too long; max key length is 1000 bytes
    【laravel】Eloquent 模型事件和监听方式
    angular使用forRoot() 注册单一实例服务
    js判断电脑是windows系统还是mac系统
    扁平数据根据`parentId`生成树结构
    页面滚动到指定元素区域
    js简洁模式代码
    简单git使用命令
    图片懒加载 echo.js
    页面图片预加载与懒加载
  • 原文地址:https://www.cnblogs.com/djh5520/p/12985417.html
Copyright © 2011-2022 走看看