转到其他逻辑驱动器并继续搜索文件

时间:2013-08-08 07:10:50

标签: c++ for-loop

我有一个程序在特定的逻辑驱动器(C :)中搜索特定扩展名(.apk)的文件。我的系统还有3个分区: - D:E:F:这些还包含apk文件。现在我想我的程序也将在这些逻辑驱动器中搜索apk的文件。我怎么能做到这一点。请任何人有一些建议然后帮助我,我从早上开始尝试这个。这是我的代码.....

int SearchDirectory(std::vector<std::string> &refvecFiles,
                    const std::string        &refcstrRootDirectory,
                    const std::string        &refcstrExtension,
                    bool                     bSearchSubdirectories = true)
{
    std::string     strFilePath;             // Filepath
    std::string     strPattern;              // Pattern
    std::string     strExtension;            // Extension
    HANDLE          hFile;                   // Handle to file
    WIN32_FIND_DATA FileInformation;         // File information


    strPattern = refcstrRootDirectory + "\\*.*";

    hFile = FindFirstFile(strPattern.c_str(), &FileInformation);
    if(hFile != INVALID_HANDLE_VALUE)
    {
        do
        {
            if(FileInformation.cFileName[0] != '.')
            {
                strFilePath.erase();
                strFilePath = refcstrRootDirectory + "\\" + FileInformation.cFileName;

                if(FileInformation.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
                {
                    if(bSearchSubdirectories)
                    {
                        // Search subdirectory
                        int iRC = SearchDirectory(refvecFiles,
                                                  strFilePath,
                                                  refcstrExtension,
                                                  bSearchSubdirectories);
                        if(iRC)
                            return iRC;
                    }
                }
                else
                {
                    // Check extension
                    strExtension = FileInformation.cFileName;
                    strExtension = strExtension.substr(strExtension.rfind(".") + 1);

                    if(strExtension == refcstrExtension)
                    {
                        // Save filename
                        refvecFiles.push_back(strFilePath);
                    }
                }
            }
        } while(FindNextFile(hFile, &FileInformation) == TRUE);

        // Close handle
        FindClose(hFile);

        DWORD dwError = GetLastError();
        if(dwError != ERROR_NO_MORE_FILES)
            return dwError;
    }

    return 0;
}


int main()
{
    int iRC = 0;
    std::vector<std::string> vecAPKFiles;
    //std::vector<std::string> vecTxtFiles;


    // Search 'c:' for '.apk' files including subdirectories
    iRC = SearchDirectory(vecAPKFiles, "c:", "apk");
    if(iRC)
    {
        std::cout << "Error " << iRC << std::endl;
        return -1;
    }

    // Print results
    for(std::vector<std::string>::iterator iterAvi = vecAPKFiles.begin();
            iterAvi != vecAPKFiles.end();
            ++iterAvi)
        std::cout << *iterAvi << std::endl;

    TCHAR szDrive[] = (" A:");
    DWORD uDriveMask = GetLogicalDrives();

    while(uDriveMask)
    {
        // Use the bitwise AND, 1â€"available, 0-not available
        if(uDriveMask & 1)
            printf("%s ", (const char *)szDrive);
        // increment, check next drive
        ++szDrive[1];
        // shift the bitmask binary right
        uDriveMask >>= 1;
    }
    printf("\n ");



    // Wait for keystroke
    _getch();
    return 0;
}

1 个答案:

答案 0 :(得分:0)

你有一些奇怪的驱动字符串char szDrive[] = " A:";(即使删除了半生不熟的TCHAR内容)。我改为使用char szDrive[] = "A:\";,并增加++szDrive[0];。然后,您可以将szDrive传递给SearchDirectory()