简体   繁体   English

QT 如何使用 QDirIterator 访问子目录中的特定目录?

[英]QT How to access specific directory within subdirectories with a QDirIterator?

I have thousands of files to go through with a directory structure like this:我有数千个文件需要处理,目录结构如下:

YYYY_MM_DD
    XXX
    XXX
    XXX
    Target
        .hdr files
    XXX
    XXX
        more .hdr files but do not want to process
YYYY_MM_DD
    XXX
    XXX
    XXX
    Target
        .hdr files
    XXX
    XXX
         more .hdr files but do not want to process

I have three months of data to go through and I need to get to the Target folder, and the Target folder only.我有三个月的数据要处理,我需要访问 Target 文件夹,并且只需要访问 Target 文件夹。 They contain the files that we need to look at and some of the other folders have .hdr files as well that we do not need to look at.它们包含我们需要查看的文件,其他一些文件夹也包含我们不需要查看的 .hdr 文件。

At first, I used a QDirIterator such as:起初,我使用了一个QDirIterator例如:

QDirIterator it(inputDir, QStringList() << "*.hdr", QDir::Files, QDirIterator::Subdirectories);

However upon running my program I realized that this was going to grab the other .hdr files as well and I do not need to process those files.然而,在运行我的程序时,我意识到这也会抓取其他.hdr文件,我不需要处理这些文件。 This is the code that I wrote that would find all of the .hdr files:这是我编写的用于查找所有.hdr文件的代码:

QDirIterator it(inputDir, QStringList() << "*.hdr", QDir::Files, QDirIterator::Subdirectories);
std::vector<std::string> files;

while(it.hasNext())
{   
    files.push_back(it.next().toStdString());
}

return files;

How can I traverse the directories but only take the files within the Target folder?如何遍历目录但只获取 Target 文件夹中的文件?

Probably give a try as said below.可能会尝试如下所述。 It is rough code, may be some more validations required.这是粗略的代码,可能需要更多的验证。

//FILTER THE SPECIFIC TARGET DIRECTORY.
QStringList filterFolder = {"TARGETFOLDERNAME"};
QDir directory("BASE DIRECTORY OF SEARCH");
QFileInfoList foldersTobeIterated = directory.entryInfoList(filterFolder);

//ITERATE THE FILTERED DIRECTORIES.
std::vector<std::string> files;
for(auto folder : foldersTobeIterated)
{
    QDirIterator it(folder.absoluteFilePath(), QStringList() <<"*.txt", QDir::Files,QDirIterator::Subdirectories);
    while(it.hasNext())
    {
        files.push_back(it.next().toStdString());
    }
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM