在C++中搜索一个文件

问题描述 投票:0回答:1

我是C++的新手,我需要搜索驱动器中的特定文件,并在列表框中显示或列出它们。

这是我目前所拥有的东西。我在论坛上找到的一些零碎的东西和我添加的东西。

我的问题是我如何把它们放在一起。 假设我在搜索一个叫 "test.txt "的文件。

谢谢!我是C++的新手,我需要搜索驱动器中的特定文件,并在列表框中显示或列出它们。

// search for drives

char* szSingleDrive;
DWORD dwSize = MAX_PATH;
char szLogicalDrives[MAX_PATH] = { 0 };
DWORD dwResult = GetLogicalDriveStrings(dwSize, szLogicalDrives);
UINT logicalDrive = GetDriveType(szLogicalDrives);

bool IsPhysicalDrive(string drives)
{
    if (logicalDrive == 3)
    {
        return logicalDrive;
    }
}

// function to look thru directories.

void FindFile(std::string directory)
{
    std::string tmp = directory + "\\*";
    WIN32_FIND_DATA file;
    HANDLE search_handle = FindFirstFile(tmp.c_str(), &file);
    if (search_handle != INVALID_HANDLE_VALUE)
    {
        std::vector<std::wstring> directories;
        do
        {                                                   
            //std::wcout << file.cFileName << std::endl;
            //::MessageBox(NULL, file.cFileName, "", MB_OK); // message box to verify that it's working
            if (file.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
            {
                if ((!lstrcmp(file.cFileName, ".")) || (!lstrcmp(file.cFileName, "..")))
                    continue;
            }
            //std::wcout << file.cFileName << std::endl;
            ::MessageBox(NULL, file.cFileName, "", MB_OK);
            directory += "\\" + std::string(file.cFileName);
            FindFile(directory);
        } 
        while (FindNextFile(search_handle, &file));

        if (GetLastError() != 18) // Error code 18: No more files left
            FindClose(search_handle);
            //CloseHandle(search_handle);

    }
}


FindFile("C:\\");  //  <-- this should spin thru different drives (physical drives from the function)

c++ winapi search directory find
1个回答
0
投票

试试像这样的东西。

void FindFile(const std::string &directory)
{
    std::string dir = directory;
    if ((!dir.empty()) && (dir.back() != '\\') && (dir.back() != '/'))
        dir += '\\';

    WIN32_FIND_DATAA file;
    HANDLE search_handle = FindFirstFileA((dir + "*").c_str(), &file);
    if (search_handle == INVALID_HANDLE_VALUE)
    {
        if (GetLastError() != ERROR_FILE_NOT_FOUND)
        {
            // error handling...
        }
    }
    else
    {
        //std::vector<std::string> subdirs;

        do
        {                                                   
            if (file.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
            {
                if ((lstrcmpA(file.cFileName, ".") != 0) && (lstrcmpA(file.cFileName, "..") != 0))
                {
                    //std::cout << file.cFileName << std::endl;
                    ::MessageBox(NULL, file.cFileName, "", MB_OK);
                    //subdirs.push_back(file.cFileName);
                    FindFile(dir + file.cFileName);
                }
            }
            else
            {
                if (lstrcmpA(file.cFileName, "test.txt") == 0)
                {
                    ::MessageBox(NULL, dir.c_str(), "FOUND IN DIR", MB_OK);
                }
            }
        } 
        while (FindNextFileA(search_handle, &file));

        if (GetLastError() != ERROR_NO_MORE_FILES)
        {
            // error handling...
        }

        FindClose(search_handle);

        /*
        for(size_t i = 0; i < subdirs.size(); ++i)
            FindFile(dir + subdirs[i]);
        */
    }
}

然后你可以这样做


DWORD dwSize = MAX_PATH;
char szLogicalDrives[MAX_PATH];
DWORD dwResult = GetLogicalDriveStrings(dwSize, szLogicalDrives);
if (dwResult == 0)
{
    // error handling...
}
else if (dwResult > MAX_PATH)
{
    // not enough buffer space...
}
else
{
    for(char* szSingleDrive = szLogicalDrives; *szSingleDrive != 0; szSingleDrive += (lstrlenA(szSingleDrive)+1))
    {
        if (GetDriveTypeA(szSingleDrive) == DRIVE_FIXED)
            FindFile(szSingleDrive);
    }
}

或者这个


DWORD dwLogicalDrives = GetLogicalDrives();
if (dwLogicalDrives == 0)
{
    // error handling...
}
else
{
    char szSingleDrive[] = "_:\\";
    for (DWORD i = 0; i < 26; ++i)
    {
        if (dwLogicalDrives & (1 << i))
        {
            szSingleDrive[0] = 'A' + i;
            if (GetDriveTypeA(szSingleDrive) == DRIVE_FIXED)
                FindFile(szSingleDrive);
        }
    }
}

0
投票

你可以用 std::filesystem 与C++17一起使用,注意这可能在许多C++17之前的编译器上可用,因为它是一个可选的规范。你也可以使用 boost::filesystem 作为替换品,或 这个 基于C++17的纯头库。filesystem 规格

要以递归方式找到单个文件,可以使用 std::filesystem 你会做这样的事情。

std::filesystem::path top_level_dir("/");

for (const auto& file_iter : std::filesystem::recursive_directory_iterator(top_level_dir) {
  if (!std::filesystem::is_directory(file_iter.path()) {
    if (file_iter.path() == search_filename) {
      // Here we have found the file
    }
  }
}

(search_filename 是要搜索的文件的文件名)

这就是 遥远的 比使用windows API更简洁,也更容易移植(请注意,我在 top_level_dir).


0
投票

您也可以使用 Windows搜索 服务而不是递归函数。这样会提高很多搜索速度。

首先,你需要确保对驱动器进行索引。Control Panel > Index Option > Modify, check the dirves.

然后你可以用Windows搜索作为样本。

#include <windows.h>
#include <searchapi.h>
#include <iostream>
#include <atldbcli.h>

using namespace std;

class CMyAccessor
{
public:
    WCHAR _szItemUrl[2048];
    __int64 _size;
    BEGIN_COLUMN_MAP(CMyAccessor)
        COLUMN_ENTRY(1, _szItemUrl)
        COLUMN_ENTRY(2, _size)
    END_COLUMN_MAP()
};
HRESULT GetSQLStringFromParams(LCID lcidContentLocaleParam,
    PCWSTR pszContentPropertiesParam,
    LCID lcidKeywordLocaleParam,
    LONG nMaxResultsParam,
    PCWSTR pszSelectColumnsParam,
    PCWSTR pszSortingParam,
    SEARCH_QUERY_SYNTAX sqsSyntaxParam,
    SEARCH_TERM_EXPANSION steTermExpansionParam,
    PCWSTR pszWhereRestrictionsParam,
    PCWSTR pszExprParam,
    PWSTR* ppszSQL)
{
    ISearchQueryHelper* pQueryHelper;

    // Create an instance of the search manager
    ISearchManager* pSearchManager;
    HRESULT hr = CoCreateInstance(__uuidof(CSearchManager), NULL, CLSCTX_LOCAL_SERVER, IID_PPV_ARGS(&pSearchManager));
    if (SUCCEEDED(hr))
    {
        // Get the catalog manager from the search manager
        ISearchCatalogManager* pSearchCatalogManager;
        hr = pSearchManager->GetCatalog(L"SystemIndex", &pSearchCatalogManager);

        if (SUCCEEDED(hr))
        {
            // Get the query helper from the catalog manager
            hr = pSearchCatalogManager->GetQueryHelper(&pQueryHelper);
            if (SUCCEEDED(hr))
            {
                hr = pQueryHelper->put_QueryContentLocale(lcidContentLocaleParam);
                if (SUCCEEDED(hr))
                {
                    hr = pQueryHelper->put_QueryContentProperties(pszContentPropertiesParam);
                }
                if (SUCCEEDED(hr))
                {
                    hr = pQueryHelper->put_QueryKeywordLocale(lcidKeywordLocaleParam);
                }
                if (SUCCEEDED(hr))
                {
                    hr = pQueryHelper->put_QueryMaxResults(nMaxResultsParam);
                }
                if (SUCCEEDED(hr))
                {
                    hr = pQueryHelper->put_QuerySelectColumns(pszSelectColumnsParam);
                }
                if (SUCCEEDED(hr))
                {
                    hr = pQueryHelper->put_QuerySorting(pszSortingParam);
                }
                if (SUCCEEDED(hr))
                {
                    hr = pQueryHelper->put_QuerySyntax(sqsSyntaxParam);
                }
                if (SUCCEEDED(hr))
                {
                    hr = pQueryHelper->put_QueryTermExpansion(steTermExpansionParam);
                }
                if (SUCCEEDED(hr))
                {
                    hr = pQueryHelper->put_QueryWhereRestrictions(pszWhereRestrictionsParam);
                }
                if (SUCCEEDED(hr))
                {
                    hr = pQueryHelper->GenerateSQLFromUserQuery(pszExprParam, ppszSQL);
                }
                pQueryHelper->Release();
            }
            pSearchCatalogManager->Release();
        }
        pSearchManager->Release();
    }
    return hr;
}


void WindowsSearch(PCWSTR indexed_drive)
{
    PWSTR pszSQL;
    wstring script = L"AND SCOPE ='file:///";
    script += indexed_drive;
    script += L"'";
    HRESULT hr = GetSQLStringFromParams(1033, L"", 1033, -1, L"System.ItemPathDisplay, System.Size",
        L"", SEARCH_ADVANCED_QUERY_SYNTAX, SEARCH_TERM_NO_EXPANSION, script.c_str(),
        L"FileName:test.txt", &pszSQL);

    if (SUCCEEDED(hr))
    {
        wcout << L"Generated query: " << pszSQL << endl;

        CDataSource cDataSource;
        hr = cDataSource.OpenFromInitializationString(L"provider=Search.CollatorDSO.1;EXTENDED PROPERTIES='Application=Windows'");
        if (SUCCEEDED(hr))
        {
            CSession cSession;
            hr = cSession.Open(cDataSource);
            if (SUCCEEDED(hr))
            {
                // cCommand is derived from CMyAccessor which has binding information in column map
                // This allows ATL to put data directly into apropriate class members.
                CCommand<CAccessor<CMyAccessor>, CRowset> cCommand;
                hr = cCommand.Open(cSession, pszSQL);
                if (SUCCEEDED(hr))
                {
                    __int64 maxValue = 0;
                    __int64 minValue = ULONG_MAX;

                    for (hr = cCommand.MoveFirst(); S_OK == hr; hr = cCommand.MoveNext())
                    {
                        wcout << cCommand._szItemUrl << L": " << cCommand._size << L" bytes" << endl;

                        maxValue = max(maxValue, cCommand._size);
                        minValue = min(minValue, cCommand._size);
                    }

                    wcout << L"Max:" << maxValue << L"Min:" << minValue << endl;

                    cCommand.Close();
                }
                cCommand.ReleaseCommand();
            }
        }
        CoTaskMemFree(pszSQL);
    }
}

int main()
{
    DWORD dwLogicalDrives = GetLogicalDrives();
    HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED | COINIT_DISABLE_OLE1DDE);
    if (SUCCEEDED(hr) && dwLogicalDrives != 0)
    {
        WCHAR szSingleDrive[] = L"_:\\";
        for (DWORD i = 0; i < 26; ++i)
        {
            if (dwLogicalDrives & (1 << i))
            {
                szSingleDrive[0] = 'A' + i;
                if (GetDriveTypeW(szSingleDrive) == DRIVE_FIXED)
                    WindowsSearch(szSingleDrive);
            }
        }

        CoUninitialize();
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.