3

我有一个程序可以在特定的逻辑驱动器(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;
}
4

1 回答 1

0

你有一个有点奇怪的驱动器字符串char szDrive[] = " A:";(即使删除了半生不熟的 TCHAR 东西)。我会char szDrive[] = "A:\";改用,并增加++szDrive[0];. 然后你可以传递szDriveSearchDirectory()

于 2013-08-08T11:26:15.213 回答