8

どこが間違っているのかわかりません。この現在のコードは、closefile に直接スキップします。ファイルを処理していません。明らかな何かが欠けているだけかもしれません。長い一日でした。

私の機能は、特定のファイルのハードディスク (c:) を検索するためのものです。EG example.txt. ここの &strFilePath は、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;
}
4

2 に答える 2

10

コードにはかなりの数のロジックバグがあります。代わりにこれを試してください(使用しているコンパイラを指定しなかったため、大文字のSStringクラスを持つC ++ Builderを想定しています。別のコンパイラを使用している場合は、必要に応じてコードを調整してください)。

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:\\", ...);

更新:その代替実装では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;
}
于 2013-02-25T21:00:09.323 に答える
3

あなたの状態は正しくありません。 と比較する必要がありますINVALID_HANDLE_VALUE

 if ( hFile != INVALID_HANDLE_VALUE)

によって返された最初の一致するファイルをスキップすることに加えてFindFirstFile、それはあなたが望むものですか?

また、ワイルドカードが必要だと思います。そうしないと、それ自体c:\\*にのみ一致しますc:\\

hFile = FindFirstFile("C:\\*", &file);
于 2013-02-25T13:58:31.773 に答える