c++ - 在整个磁盘上查找带有通配符的文件

标签 c++

我需要在磁盘上找到一个使用通配符(*.txt*.jpeg*.doc)的可变扩展文件(c:).

我试过以下函数,但我不能使它递归,而且它不起作用:

void Recurse(LPCTSTR str)
{
    WIN32_FIND_DATA file;
    cout << "Value INPUT:" << str <<"\n";
    HANDLE search_handle=FindFirstFile(str,&file);
    if (search_handle) {
        do {
        if(file.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY &&
                strcmp(file.cFileName,"..")!=0
                && strcmp(file.cFileName,".")!=0
                && strcmp(file.cFileName,"$Recycle.Bin")!=0
                && strcmp(file.cFileName,"Recovery")!=0
                && strcmp(file.cFileName,"System Volume Information")!=0
                && strcmp(file.cFileName,"PerfLogs")!=0
                && strcmp(file.cFileName,"Windows")!=0)
            {
                str = file.cFileName;
                cout << "Directory :" << file.cFileName <<"\n";
                Recurse(str); // Function to remind new directory
            } else {
                if (file.cFileName == "*.txt") {
                    std::wcout << file.cFileName << std::endl; //Print files find with estension txt
                }
            }
        } while(FindNextFile(search_handle,&file));

        CloseHandle(search_handle);
    }
}

当然我用一个静态值(*.txt)试了一下看它是否有效,但是当我从 main 中调用它时它不起作用:

Recurse("C:\\*");

最佳答案

I modified the code in this way. If it finds them throwing me * .txt files and me brings them correctly, but the path instead in many files shows me incorrect with several strange characters. I can not understand why such behavior.  

 char *my1;
    char *my2;
    char *my3;
    char *my4;
    char *cp2;
    LPCTSTR ori;
    std::string stringa_mia;
    std::vector<std::string> listafiles;
    std::vector<std::string> listapath;

    void Recurse(LPCTSTR str, LPCTSTR ori)
    {
        WIN32_FIND_DATA file;
        cp2 = strdup(str);
        strcat(cp2, "\\*");
        HANDLE search_handle=FindFirstFile(cp2,&file);
        BOOL res = TRUE;
        if (search_handle)
        {
            while(search_handle != INVALID_HANDLE_VALUE && res)
            {
            if(file.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY &&
                        strcmp(file.cFileName,"..")!=0
                        && strcmp(file.cFileName,".")!=0
                        && strcmp(file.cFileName,"$Recycle.Bin")!=0
                        && strcmp(file.cFileName,"Recovery")!=0
                        && strcmp(file.cFileName,"System Volume Information")!=0
                        && strcmp(file.cFileName,"PerfLogs")!=0
                        && strcmp(file.cFileName,"Windows")!=0
                        && strcmp(file.cFileName,"Program Files")!=0
                        && strcmp(file.cFileName,"Boot")!=0
                        && strcmp(file.cFileName,"Program Files (x86)")!=0
                        && strcmp(file.cFileName,"AppData")!=0
                        && strcmp(file.cFileName,"ProgramData")!=0
                        && strcmp(file.cFileName,"Cookies")!=0
                        && strcmp(file.cFileName,".android")!=0
                        && strcmp(file.cFileName,"Programmi")!=0
                        )
                    {
                        my3 = strdup(file.cFileName);
                        my2 = strdup(str);
                        my1 = strdup(ori);
                        strcat(my2, "\\");
                        strcat(my2, my3);
                        CloseHandle(search_handle);
                        Recurse(my2,ori);
                        }
            else
            {
                my4 = strdup(file.cFileName);
                stringa_mia = std::string(my4);
                if (stringa_mia.find(".txt")!=std::string::npos) {
                  listafiles.push_back(stringa_mia);
                  listapath.push_back(str);
                                                }
             }
            res = FindNextFile(search_handle,&file);
            }
                CloseHandle(search_handle);

        }
    }

我在这种模式下调用函数:

递归("C:","C:");

关于c++ - 在整个磁盘上查找带有通配符的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36543028/

相关文章:

c++ - SDL/TTF : TTF_RenderText_Blended_Wrapped returns NULL

c++ - Blitz++ 数组作为全局数组

c++ - sizeof() 的值是由编译器还是链接器决定的?

c++ - QT C++ 中的 QStringList

c++ - 通过带有成员函数指针的 QHash 调用成员函数的正确方法

c++ - 将 HEX 转换为可打印的字符串/字符

c++ - 将 POD 结构/结构 vector 复制到 vector<unsigned char> 的最优雅方式

c++ - 如何在数组的一部分中查找特定值的第一个元素

c++ - 我不能在调用 istream 作为参数的函数中使用 ifstream 吗?

c++ - 如何调试 regasm(注册哪些类型)