通配符和IShellFolder枚举?

时间:2019-06-11 02:45:36

标签: shell winapi com

我想尝试离开FindFirstFile() / FindNextFile(),而直接与IShellFolder合作以获取文件列表。原因是我想通过每个文件的IShellItem获得一个SHNCreateItemFromIDList(),并且我拥有传递给该函数所需的一切。如果使用文件路径功能,我认为每次传递给SHCreateItemFromParsingName()之前都需要构建完整路径,但是我会问另一个与此相关的问题。

我的问题仅限于使用IShellFolder来使用通配符枚举文件和文件夹。有内置的功能吗?还是必须自己进行文件匹配?

TIA !!

2 个答案:

答案 0 :(得分:2)

您不能使用IShellFolder进行过滤,但是可以使用命令行管理程序中内置的搜索工具以编程方式执行相同的操作,就像使用Windows资源管理器UI一样。

例如,您可以在右上角的搜索框中键入类似 ext:.txt 的内容,这意味着您要过滤所有包含 .txt 扩展名:

enter image description here

这是一些等效的C ++示例代码(我已经删除了每一行的错误检查,但请确保您测试了所有可能的错误):

int main()
{
  CoInitialize(NULL);
  {
    CComPtr<ISearchFolderItemFactory> search;
    CComPtr<IShellItem> item;
    CComPtr<IShellItemArray> items;
    CComPtr<IQueryParserManager> mgr;
    CComPtr<IQueryParser> parser;
    CComPtr<IQuerySolution> solution;
    CComPtr<ICondition> condition;
    CComPtr<IShellItem> searchItem;
    CComPtr<IEnumShellItems> enumItems;

    // create search folder factory
    search.CoCreateInstance(CLSID_SearchFolderItemFactory);

    // create d:\temp shell item and set search folder scope to it
    SHCreateItemFromParsingName(L"d:\\temp", NULL, IID_PPV_ARGS(&item));
    SHCreateShellItemArrayFromShellItem(item, IID_PPV_ARGS(&items));
    search->SetScope(items);

    // create the query parser manager
    mgr.CoCreateInstance(CLSID_QueryParserManager);
    mgr->CreateLoadedParser(L"", 0, IID_PPV_ARGS(&parser));

    // parse an ms-search expression
    parser->Parse(L"ext:.txt", NULL, &solution);

    // get the condition the parser has built for us
    solution->GetQuery(&condition, NULL);

    // give the condition to the search folder factory
    search->SetCondition(condition);

    // get the search result back as a shell item (a virtual folder) and enumerates it
    search->GetShellItem(IID_PPV_ARGS(&searchItem));
    searchItem->BindToHandler(NULL, BHID_EnumItems, IID_PPV_ARGS(&enumItems));

    do
    {
      CComPtr<IShellItem> child;
      ULONG fetched;
      HRESULT hr2 = enumItems->Next(1, &child, &fetched);
      if (!fetched)
        break;

      // get the display name (for example)
      CComHeapPtr<WCHAR> name;
      child->GetDisplayName(SIGDN_NORMALDISPLAY, &name);
      wprintf(L"item: %s\n", name);

      CComHeapPtr<WCHAR> path;
      child->GetDisplayName(SIGDN_FILESYSPATH, &path);
      wprintf(L" path: %s\n", path);
    } while (TRUE);
  }
  CoUninitialize();
  return 0;
}

search-ms 语言非常强大。其语法在此处可用:Querying the Index with the search-ms Protocol

答案 1 :(得分:0)

shell API提供了枚举,但没有类似于FindFirstFile() / FindNextFile()的任何过滤或通配符功能。

因此,在使用外壳程序枚举功能时,必须手动过滤项目。