递归硬盘搜索FindFirstFile& FindNextFile C ++ [英] Recursive hard disk search with FindFirstFile & FindNextFile C++

查看:139
本文介绍了递归硬盘搜索FindFirstFile& FindNextFile C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没有看到我会出错。这个当前代码直接跳到closefile。 NOt处理任何文件,我可能只是缺少一些明显的东西,它已经是一个漫长的一天。

I am failing to see where i am going wrong. This current code skips straight to closefile. NOt processing any files, i may just be missing something obvious and it has been a long day.

我的功能用于搜索给定文件的硬盘(c :)。 EG example.txt。 & strFilePath将在FindFirstFile声明中使用。

My function is meant to search the hard disk (c:) for a given file. EG example.txt. &strFilePath here would be used in the FindFirstFile declaration.

任何帮助将得到满足。

Any help would be appeciated.

感谢。

String Copy::SearchDrive( const String& strFile, const String& strFilePath, const bool& bRecursive, const bool& bStopWhenFound ) const
{
    HANDLE hFile;

    WIN32_FIND_DATA file;

    hFile = FindFirstFile("C:\\", &file);

    String strFoundFilePath = "";

    if ( hFile )
    {
        while ( FindNextFile( hFile, &file))
        {
            String strTheNameOfTheFile = file.cFileName;
            // It could be a directory we are looking at
            // if so look into that dir
            if ( file.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY
                && bRecursive )
            {
                String strNewFilePath = strFilePath + "\\";
                strNewFilePath += strTheNameOfTheFile;
                SearchDrive( strFile, strNewFilePath, bRecursive, bStopWhenFound );
            }
            else
            {
                if ( strTheNameOfTheFile == strFile )
                {
                    strFoundFilePath = strFilePath;
                    strFoundFilePath += "\\";
                    strFoundFilePath += strFile;

                    /// TODO
                    // ADD TO COLLECTION TYPE

                    if ( bStopWhenFound )
                    {
                        break;
                    }
                }
            }
        }
        CloseHandle( hFile );
    }
    return strFoundFilePath;
}


推荐答案

代码中的错误。请尝试这样做(你没有指明使用哪个编译器,所以我假设C ++ Builder,它有一个大写的 String 类。根据需要调整代码如果你使用不同的编译器):

You have quite a few logic bugs in your code. Try this instead (you did not indicate which compiler you are using, so I am assuming C++Builder, which has an uppercase-S String class. Adjust the code as needed if you are using a different compiler):

String Copy::SearchDrive(const String& strFile, const String& strFilePath, const bool& bRecursive, const bool& bStopWhenFound) const
{
    String strFoundFilePath;
    WIN32_FIND_DATA file;

    String strPathToSearch = strFilePath;
    if (!strPathToSearch.IsEmpty())
        strPathToSearch = IncludeTrailingPathDelimiter(strPathToSearch);

    HANDLE hFile hFile = FindFirstFile((strPathToSearch + "*").c_str(), &file);
    if (hFile != INVALID_HANDLE_VALUE)
    {
        do
        {
            String strTheNameOfTheFile = file.cFileName;

            // It could be a directory we are looking at
            // if so look into that dir
            if (file.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
            {
                if ((strTheNameOfTheFile != ".") && (strTheNameOfTheFile != "..") && (bRecursive))
                {
                    strFoundFilePath = SearchDrive(strFile, strPathToSearch + strTheNameOfTheFile, bRecursive, bStopWhenFound);

                    if (!strFoundFilePath.IsEmpty() && bStopWhenFound)
                        break;
                }
            }
            else
            {
                if (strTheNameOfTheFile == strFile)
                {
                    strFoundFilePath = strPathToSearch + strFile;

                    /// TODO
                    // ADD TO COLLECTION TYPE

                    if (bStopWhenFound)
                        break;
                }
            }
        }
        while (FindNextFile(hFile, &file));

        FindClose(hFile);
    }

    return strFoundFilePath;
}

String strFoundFilePath = SearchDrive("file.ext", "C:\\", ...);

UPDATE: SearchDrive ),它在通过子目录递归时不会保持多个搜索句柄打开:

UPDATE: An alternative implementation of SearchDrive() that does not keep multiple search handles open while recursing through sub-directories:

#include <memory>

String Copy::SearchDrive(const String& strFile, const String& strFilePath, const bool& bRecursive, const bool& bStopWhenFound) const
{
    String strFoundFilePath;
    WIN32_FIND_DATA file;

    String strPathToSearch = strFilePath;
    if (!strPathToSearch.IsEmpty())
        strPathToSearch = IncludeTrailingPathDelimiter(strPathToSearch);

    HANDLE hFile hFile = FindFirstFile((strPathToSearch + "*").c_str(), &file);
    if (hFile != INVALID_HANDLE_VALUE)
    {
        std::auto_ptr<TStringList> subDirs;

        do
        {
            String strTheNameOfTheFile = file.cFileName;

            // It could be a directory we are looking at
            // if so look into that dir
            if (file.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
            {
                if ((strTheNameOfTheFile != ".") && (strTheNameOfTheFile != "..") && (bRecursive))
                {
                    if (subDirs.get() == NULL)
                        subDirs.reset(new TStringList);

                    subDirs->Add(strPathToSearch + strTheNameOfTheFile);
                }
            }
            else
            {
                if (strTheNameOfTheFile == strFile)
                {
                    strFoundFilePath = strPathToSearch + strFile;

                    /// TODO
                    // ADD TO COLLECTION TYPE

                    if (bStopWhenFound)
                        break;
                }
            }
        }
        while (FindNextFile(hFile, &file));

        FindClose(hFile);

        if (!strFoundFilePath.IsEmpty() && bStopWhenFound)
            return strFoundFilePath;

        if (subDirs.get() != NULL)
        {
            for (int i = 0; i < subDirs->Count; ++i)
            {
                strFoundFilePath = SearchDrive(strFile, subDirs->Strings[i], bRecursive, bStopWhenFound);

                if (!strFoundFilePath.IsEmpty() && bStopWhenFound)
                    break;
            }
        }
    }

    return strFoundFilePath;
}

这篇关于递归硬盘搜索FindFirstFile&amp; FindNextFile C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆