Search for a file in C++

二次信任 提交于 2021-01-29 16:12:51

问题


I'm new in C++ and i need to search the drives for a specific file and display them or list them in a listbox.

Here's what i have so far. Some bits and pieces i found in the forums and stuff i have added.

My question is how do i put them together. Lets say i'm searching for a file called "test.txt"

Thanks!

// 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)


回答1:


Try something more like this:

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]);
        */
    }
}

And then you can do this:


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);
    }
}

Or this:


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);
        }
    }
}



回答2:


You can use std::filesystem with C++17, note that this may be available on many compilers pre-C++17 since it is an optional specification. You can also use boost::filesystem as a drop in replacement, or this header-only library based on the C++17 filesystem specification

To find a single file recursively with std::filesystem you would do something like this:

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 is the filename of the file to search for)

This is far more concise than using the windows API, as well as being more portable (Notice how I use a forward slash in the path for top_level_dir).




回答3:


You could also use Windows Search Service instead of recursive function. This will increase the search speed a lot.

First, you need to make sure to index the drive: Control Panel > Index Option > Modify, check the dirves.

Then you could use Windows Search as the sample:

#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();
    }
}


来源:https://stackoverflow.com/questions/61375911/search-for-a-file-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!