通过扩展名搜索特定文件夹Winapi32



我正在尝试创建一个搜索目录,但同时。我想在多个目录上搜索特定文件,例如.txt ,and .jpg,但是如何实现此目标?这就是我到目前为止得到的

int _tmain(int argc, TCHAR* argv[])
{
    WIN32_FIND_DATA ffd;
    LARGE_INTEGER filesize;
    TCHAR szDir[MAX_PATH];
    size_t length_of_arg;
    HANDLE hFind = INVALID_HANDLE_VALUE;
    DWORD dwError = 0;

    StringCchCopy(szDir, MAX_PATH, L"C:\Users\USER-U\Documents\aaa\project1\Release\");
    StringCchCat(szDir, MAX_PATH, TEXT("\*"));
    // Find the first file in the directory.
    hFind = FindFirstFile(szDir, &ffd);
    // List all the files in the directory with some info about them.
    do
    {
        if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
        {
            _tprintf(TEXT("  %s   <DIR>n"), ffd.cFileName);
        }
        else
        {
            filesize.LowPart = ffd.nFileSizeLow;
            filesize.HighPart = ffd.nFileSizeHigh;
            _tprintf(TEXT("  %s   %ld bytesn"), ffd.cFileName, filesize.QuadPart);
        }
    } while (FindNextFile(hFind, &ffd) != 0);
    dwError = GetLastError();

    FindClose(hFind);
    return dwError;
}

如果要搜索多种文件类型,则不支持它,但是可以使用*.*并过滤结果。如果要检索子目录,则可以使用递归函数来检索(如果当前搜索是目录(,我将稍后更新该示例。

更新:

#include <windows.h>
#include <iostream>
#include <tchar.h>
#pragma warning (disable: 4996)
void retrievetxt(TCHAR* szDir)
{
    WIN32_FIND_DATA ffd;
    LARGE_INTEGER filesize;
    _tcscat(szDir, _T("\*"));
    HANDLE hFind = FindFirstFile(szDir, &ffd);
    // List all the files in the directory with some info about them.
    do
    {
        if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
        {
            if (!_tcscmp(ffd.cFileName, _T(".")) || !_tcscmp(ffd.cFileName, _T("..")))
                continue;
            TCHAR szDircpy[MAX_PATH] = { 0 };
            _tcscpy(szDircpy, szDir);
            szDircpy[_tcslen(szDircpy)-1] = _T(''); // remove "*" character from "xxx\*"
            _tcscat(szDircpy, ffd.cFileName); //add the subdirectory name into szDir
            _tprintf(TEXT("Go into  %s\%s   <DIR>n"), szDircpy, ffd.cFileName);
            retrievetxt(szDircpy);
        }
        else
        {
            int size = _tcslen(ffd.cFileName);
            if (size > 4 && !_tcscmp(ffd.cFileName + size - _tcslen(_T(".txt")), _T(".txt")))
            {
                filesize.LowPart = ffd.nFileSizeLow;
                filesize.HighPart = ffd.nFileSizeHigh;
                _tprintf(TEXT("%s   %ld bytesn"), ffd.cFileName, filesize.QuadPart);
            }
        }
    } while (FindNextFile(hFind, &ffd) != 0);
    FindClose(hFind);
}
int _tmain(int argc, TCHAR* argv[])
{
    TCHAR szDir[MAX_PATH] = { 0 };
    _tcscpy(szDir,_T("C:\Users\drakew\Desktop\Newfolder"));
    _tprintf(TEXT("Go into  %s   <DIR>n"), szDir);
    retrievetxt(szDir);
    getchar();
    return 0;
}

如果要使用多个扩展搜索,则可以使用IsearchFolderItemFactory Interface

例如,在目录中搜索 *.jpg或 *.png的测试" e: test" =>

(参考:ExplorerBrowserearch SDK样本(

HRESULT hr = S_OK;
ISearchFolderItemFactory* pSearchFolderItemFactory = NULL;
hr = CoCreateInstance(__uuidof(SearchFolderItemFactory), NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pSearchFolderItemFactory));
if (SUCCEEDED(hr))
{
    WCHAR wsFolder[MAX_PATH] = L"e:\test";
    IShellItem *pShellItem = NULL;
    HRESULT hr = SHCreateItemFromParsingName(wsFolder, NULL, IID_PPV_ARGS(&pShellItem));
    if (SUCCEEDED(hr))
    {
        IShellItemArray* pShellItemArray = NULL;
        hr = SHCreateShellItemArrayFromShellItem(pShellItem, IID_IShellItemArray, (void**)&pShellItemArray);
        if (SUCCEEDED(hr))
        {
            hr = pSearchFolderItemFactory->SetScope(pShellItemArray);
            IQueryParserManager *pQueryParserManager = NULL;
            HRESULT hr = CoCreateInstance(__uuidof(QueryParserManager), NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pQueryParserManager));
            if (SUCCEEDED(hr))
            {
                IQueryParser *pQueryParser = NULL;
                hr = pQueryParserManager->CreateLoadedParser(L"SystemIndex", LOCALE_USER_DEFAULT, IID_PPV_ARGS(&pQueryParser));
                if (SUCCEEDED(hr))
                {
                    IQuerySolution* pQuerySolution = NULL;
                    hr = pQueryParser->Parse(L"*.jpg OR *.png", NULL, &pQuerySolution);
                    if (SUCCEEDED(hr))
                    {
                        ICondition* pCondition = NULL;
                        hr = pQuerySolution->GetQuery(&pCondition, NULL);
                        if (SUCCEEDED(hr))
                        {
                            SYSTEMTIME st;
                            GetLocalTime(&st);
                            ICondition* pConditionResult = NULL;
                            hr = pQuerySolution->Resolve(pCondition, SQRO_DONT_SPLIT_WORDS, &st, &pConditionResult);
                            if (SUCCEEDED(hr))
                            {
                                hr = pSearchFolderItemFactory->SetCondition(pConditionResult);
                                if (SUCCEEDED(hr))
                                {
                                    IShellItem* pShellItem = NULL;
                                    hr = pSearchFolderItemFactory->GetShellItem(IID_PPV_ARGS(&pShellItem));
                                    if (SUCCEEDED(hr))
                                    {
                                        IEnumShellItems *pEnumShellItems = NULL;
                                        hr = pShellItem->BindToHandler(NULL, BHID_EnumItems, IID_PPV_ARGS(&pEnumShellItems));
                                        if (SUCCEEDED(hr))
                                        {
                                            IShellItem *psi;
                                            PWSTR pszName = NULL;
                                            while (S_OK == pEnumShellItems->Next(1, &psi, NULL))
                                            {                                                           
                                                hr = psi->GetDisplayName(SIGDN_FILESYSPATH, &pszName);
                                                WCHAR wsBuffer[MAX_PATH];
                                                wsprintf(wsBuffer, L"File : %sn", pszName);
                                                OutputDebugString(wsBuffer);
                                                psi->Release();
                                            }
                                            pEnumShellItems->Release();
                                        }
                                        pShellItem->Release();
                                    }                                               
                                }
                                pConditionResult->Release();
                            }
                            pCondition->Release();
                        }
                        pQuerySolution->Release();
                    }
                    pQueryParser->Release();
                }
                pQueryParserManager->Release();
            }
            pShellItemArray->Release();
        }
        pShellItem->Release();
    }
    pSearchFolderItemFactory->Release();
}

最新更新