我正在尝试创建一个搜索目录,但与此同时。我想在多个目录中搜索诸如.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 bytes\n"), ffd.cFileName, filesize.QuadPart);
}
} while (FindNextFile(hFind, &ffd) != 0);
dwError = GetLastError();
FindClose(hFind);
return dwError;
}
答案 0 :(得分:3)
如果要搜索多种文件类型,则不支持,但是可以使用*.*
并过滤结果。
如果要检索子目录,可以使用递归函数进行检索(如果当前搜索是目录),稍后我将更新示例。
更新:
#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('\0'); // 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 bytes\n"), 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;
}
答案 1 :(得分:0)
如果要搜索多个扩展名,可以使用ISearchFolderItemFactory interface
例如,通过在目录“ e:\ test” =>
中搜索* .jpg或* .png进行测试(参考:ExplorerBrowserSearch 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 : %s\n", 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();
}