如何获取我的驱动器中具有特定扩展名的所有文件的列表?

How to get the list of all files in my drive with specific extension?

大家好我是新手。经过长期无望的搜索,我想 post 这个问题:

我正在开发一个 C++ 程序,该程序在所有文件夹和子文件夹上递归搜索特定文件类型。

首先,函数 FindFiles(string, string, bool) 非常有用,但它的第二种形式 FindFiles(struct var) 不能正常工作:它不会遍历所有文件夹和子文件夹。

事实上,我需要第二种形式,因为搜索可能太长了,我需要用 API CreateThread 创建一个线程,并将我的 arguments struct 传递给它作为 LPVOID.

#include <windows.h>
#include <string>
#include <iostream>
using namespace std;



// Arguments struct
struct args{
    string strDir;
    string strFilter;
    bool bRecurse;
};


void FindFiles(string strDir, string strFilter, bool bRecurse);
void FindFiles(args);


int main(){

//  FindFiles("D:", "*.mp3", true); // works fine

    args ar;
    ar.bRecurse  = true;
    ar.strDir    = "D:";
    ar.strFilter = "*.mp3";

    FindFiles(ar); // doesn't work fine

    cout << endl;
    return 0;
}

void FindFiles(string strDir, string strFilter, bool bRecurse = true){
    if(bRecurse)
        FindFiles(strDir, strFilter, false);
    strDir += "\";
    WIN32_FIND_DATA wfd;

    string strFileFilter = strDir + (bRecurse ? "*" : strFilter);
    HANDLE hFile = FindFirstFile(strFileFilter.c_str(), &wfd);

    if(INVALID_HANDLE_VALUE == hFile)
        return;
    else{
        if(!bRecurse)
            cout << strDir + string(wfd.cFileName) << endl;
        while(FindNextFile(hFile, &wfd) ){
            if(!bRecurse)
                cout << strDir + string(wfd.cFileName) << endl;
            else{
                if( (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) > 0 &&
                    wfd.cFileName[0] != '.')
                    FindFiles(strDir + string(wfd.cFileName), strFilter, true);
            }   
        }
    //  FindClose(hFile);
    }
}

void FindFiles(args ar){
    if(ar.bRecurse){
        ar.bRecurse = false;
        FindFiles(ar);
    }
    ar.strDir += "\";
    WIN32_FIND_DATA wfd;

    string strFileFilter = ar.strDir + (ar.bRecurse ? "*" : ar.strFilter);
    HANDLE hFile = FindFirstFile(strFileFilter.c_str(), &wfd);

    if(INVALID_HANDLE_VALUE == hFile)
        return;
    else{
        if(!ar.bRecurse)
            cout << ar.strDir + string(wfd.cFileName) << endl;
        while(FindNextFile(hFile, &wfd) ){
            if(!ar.bRecurse)
                cout << ar.strDir + string(wfd.cFileName) << endl;
            else{
                if( (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) > 0 &&
                    wfd.cFileName[0] != '.'){
                        ar.strDir += string(wfd.cFileName);
                        ar.bRecurse = true;
                        FindFiles(ar);
                }
            }   
        }
        FindClose(hFile);
    }
}

请看第二张表格,因为它是我的。我认为那里有一些愚蠢的错误。

任何帮助我都非常感谢。

您的 FindFiles() 的 1 参数版本不会递归,因为它正在修改 ar.bRecurse 标志,因此在搜索子文件夹时它始终为 false:

void FindFiles(args ar){
    if(ar.bRecurse){
        ar.bRecurse = false; // <-- modified!
        FindFiles(ar);
    }
    ...
    // ar.bRecurse is still false here!
}

FindFiles() 的 3 参数版本不会这样做:

void FindFiles(string strDir, string strFilter, bool bRecurse = true){
    if(bRecurse)
        FindFiles(strDir, strFilter, false); // <-- bRecurse is not modified here!
    ...
    // bRecurse is still the original value here!
}

最简单的解决方案是在初始 FindFiles() 调用后将 ar.bRecurse 重置为 true:

void FindFiles(args ar){
    if(ar.bRecurse){
        ar.bRecurse = false; // <-- modified!
        FindFiles(ar);
        ar.bRecurse = true; // <-- reset here!
    }
    ...
}

或者对该调用使用临时结构:

void FindFiles(args ar){
    if(ar.bRecurse){
        args arTmp = ar; // <-- temp copy!
        arTmp.bRecurse = false; // <-- modify the copy!
        FindFiles(arTmp);
    }
    ...
    // ar.bRecurse is still the original value here!
}

话虽如此,您实际上根本不需要在函数顶部进行 bRecurse 检查。它真正做的只是扫描输入文件夹以查找匹配的文件,然后再次扫描文件夹以仅查找子文件夹。您可以通过使用单个搜索并在本地缓存子文件夹以进行下一步搜索来完成相同的逻辑,例如:

#include <windows.h>

#include <string>
#include <iostream>
#include <vector>

using namespace std;

// Arguments struct
struct args
{
    string strDir;
    string strExt;
    bool bRecurse;
};

void FindFiles(const string &strDir, const string &strExt, bool bRecurse = true);
void FindFiles(const args &ar);

int main()
{    
    // FindFiles("D:", ".mp3", true);

    args ar;
    ar.strDir   = "D:";
    ar.strExt   = ".mp3";
    ar.bRecurse = true;

    FindFiles(ar);

    cout << endl;
    return 0;
}

void FindFiles(const string &strDir, const string &strExt, bool bRecurse)
{
    string strSearchDir = strDir;
    vector<string> vDirs;

    if (!strSearchDir.empty())
    {
        switch (strSearchDir[strSearchDir.size()-1])
        {
            case '\':
            case '/':
                break;
            default:
              strSearchDir += "\";
              break;
        }
    }

    WIN32_FIND_DATAA wfd = {};

    HANDLE hFile = FindFirstFileA((strSearchDir + "*.*").c_str(), &wfd);
    if (INVALID_HANDLE_VALUE == hFile)
        return;

    do
    {
        string strFilename(wfd.cFileName);

        if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
        {
            if ((strFilename == ".") || (strFilename == ".."))
                continue;

            if (bRecurse)
                vDirs.push_back(strSearchDir + strFilename);
        }
        else
        {
            if (!strExt.empty())
            {
                if (strFilename.size() < strExt.size())
                    continue;

                if (CompareStringA(LOCALE_USER_DEFAULT, NORM_IGNORECASE, strFilename.c_str()+(strFilename.size()-strExt.size()), strExt.size(), strExt.c_str(), strExt.size()) != 2)
                    continue;
            }

            cout << strSearchDir << strFilename << endl;
        }
    }
    while (FindNextFile(hFile, &wfd));

    FindClose(hFile);

    if (bRecurse)
    {
        for(vector<string>::iterator iter = vDirs.begin(), end = vDirs.end(); iter != end; ++iter)
            FindFiles(*iter, strExt, true);
    }
}

void FindFiles(const args &ar)
{
    FindFiles(ar.strDir, ar.strExt, ar.bRecurse);
}