简体   繁体   English

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

[英]go to other logical drives and continues to search for file

i have a program that search for files of a particular extention(.apk) in a particular logical drive(C:). 我有一个程序,在特定的逻辑驱动器(C :)中搜索特定扩展名(.apk)的文件。 my system has 3 more partitions :- D: E: F: and these also contains apk files. 我的系统还有3个分区: - D:E:F:这些还包含apk文件。 now i want that my program will also search in these logical drives for the apk's files. 现在我想我的程序也将在这些逻辑驱动器中搜索apk的文件。 how i can do this. 我怎么能做到这一点。 please anybody have some suggestion then help me, am trying this since morning. 请任何人有一些建议然后帮助我,我从早上开始尝试这个。 here is my code..... 这是我的代码.....

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

You've got a bit weird drive string char szDrive[] = " A:"; 你有一点奇怪的驱动器字符串char szDrive[] = " A:"; (Even with the half-baked TCHAR stuff removed). (即使删除了半生不熟的TCHAR东西)。 I'd use char szDrive[] = "A:\\"; 我用char szDrive[] = "A:\\"; instead, and increment ++szDrive[0]; 相反,并增加++szDrive[0]; . You can then pass szDrive to SearchDirectory() 然后,您可以将szDrive传递给SearchDirectory()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何获取逻辑驱动器列表? - How to get a list of logical drives? 从单元掩码中查找所有逻辑驱动器 - Find all logical drives from a unitmask 如何在C++中使用WMI获取硬盘所有逻辑驱动器的空闲空间? - How to get free space of all logical drives of hard disk using WMI in C++? 文件在套接字传输中继续接收无限数据 - File continues to receive infinite data in socket transfer 逻辑运算符是否在if语句中相互影响? - Do logical operators affect each other in a if statement? Qt C ++:移动文件,源路径和目标路径位于不同的驱动器上 - Qt C++: Moving a file, source and destination paths are on different drives Go和C ++中的指针和引用之间的逻辑差异? - logical difference between pointers and references in Go and C++? 这种树搜索功能使我发疯。 它返回NULL,但是主函数中的值以某种方式改变 - This tree search function drives me crazy. It return NULL but somehow the value changes in the main function 二进制搜索树,顺序遍历,步骤3逻辑协助 - Binary Search Tree, In Order Traversal, Step 3 logical assistance 将此实例变量添加到C ++ 11文件的标头中会使编译器难以为继。 为什么? - Adding this instance variable to the header of a C++11 file drives the compiler up the wall. Why?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM