1

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. 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.

At first, I used a QDirIterator such as:

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. This is the code that I wrote that would find all of the .hdr files:

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?

Sailanarmo
  • 1,139
  • 15
  • 41

1 Answers1

0

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());
    }
}
Pavan Chandaka
  • 11,671
  • 5
  • 26
  • 34