使用FindFirstFile&amp ;;进行递归硬盘搜索FindNextFile C ++

时间:2013-02-25 13:48:43

标签: c++ winapi

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

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

任何帮助都会被指定。

感谢。

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

2 个答案:

答案 0 :(得分:5)

你的代码中有很多逻辑错误。试试这个(你没有指出你正在使用哪个编译器,所以我假设C ++ Builder,它有一个大写的S String类。如果你使用不同的编译器,根据需要调整代码):

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 = 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()的替代实现,在通过子目录递归时不会保持多个搜索句柄处于打开状态:

#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 = 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;
}

答案 1 :(得分:2)

您的情况不正确,您应该与INVALID_HANDLE_VALUE

进行比较
 if ( hFile != INVALID_HANDLE_VALUE)

除了你跳过FindFirstFile返回的第一个匹配文件,你想要的是什么?

另外我相信您需要一个通配符c:\\*,否则它只会匹配c:\\本身

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