Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
594 views
in Technique[技术] by (71.8m points)

winapi - Recursive hard disk search with FindFirstFile & FindNextFile C++

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.

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.

Thanks.

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;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...