Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recursive hard disk search with FindFirstFile & FindNextFile C++

Tags:

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