SpellTheif
SpellTheif

Reputation: 715

Qt - how to use QDir::entryList() to filter both files with specific file types and directories at the same time

Is that possible to retrieve file names with specific file types and directory names at the same time by using QDir::entryList()?

I have had the flag QDir::Dirs in my code. But since I added << "*.mp4", it ignored the folders.

void Hierarchy::setItems(const QString& path, int level)
{
    QDir source(path);
    if (!source.exists())
        return;

    QStringList folders = source.entryList(QStringList() << "*.mp4", 
            QDir::Files | QDir::NoDot | QDir::NoDotDot | QDir::Dirs);

    for (int i = 0; i < folders.size(); ++i) {
        qDebug() << "Level " << i << " " << folders[i];
        setItems(path + "/" + folders[i] + "/", level++);
    }

}

Upvotes: 4

Views: 12151

Answers (1)

Dimitry Ernot
Dimitry Ernot

Reputation: 6594

You could create two lists: one for the folders and another for the files:

void setItems(QString const& path, int level)
{
    QDir const source(path);
    if (!source.exists())
        return;

    QStringList const files = source.entryList(QStringList() << "*.mp4", QDir::Files);
    QStringList const folders = source.entryList(QDir::NoDot | QDir::NoDotDot | QDir::Dirs);

    QStringList const all = folders + files;

    for (QString const& name: all)
    {
        QString const fullPathName = path + QDir::separator() + name;
        if (QFileInfo(fullPathName).isDir())
        {
            setItems(fullPathName, level++);
            continue;
        }
        qDebug() << "Level " << level << " " << fullPathName;
    }
}

Upvotes: 3

Related Questions