Qt recursively gets all files under the specified folder

method one

Use the class QDirIterator to traverse. The introduction
insert image description hereprobably means that it is suitable for large directory traversal, supports recursion but does not support sorting.
insert image description hereQDirIterator::NoIteratorFlagsBy default, with no flags, the iterator will return pathmatching QDir::Filtersentries. Also list the entries matching the rule
QDirIterator::Subdirectoriesunder all sub-filesQDir::Filters

QDirIterator it(dirPath, QDir::Files | QDir::NoSymLinks | QDir::NoDotAndDotDot | QDir::AllDirs);

Returns all files and subdirectories under a directory, excluding file links and. ..

QDirIterator it(dirPath, QDir::Files | QDir::NoSymLinks | QDir::NoDotAndDotDot);

Return all files in the directory (there is no sub-file file directory at this time), excluding file links and . ..
codes

void Widget::on_pushButton_clicked()
{
    
    
    QStringList list;
    QString dirPath = "F:\\dirTest";
    QDirIterator it(dirPath, QDir::Files | QDir::NoSymLinks | QDir::NoDotAndDotDot | QDir::AllDirs, QDirIterator::Subdirectories);
    while(it.hasNext())
    {
    
    
        it.next();
        if(it.fileInfo().suffix() == "icd")
        {
    
    
            list.append(it.fileInfo().absoluteFilePath());
        }
    }
    foreach(QString str, list)
    {
    
    
        qDebug() << str;
    }
}

output

"F:/dirTest/nihao/qunimade/max.icd"
"F:/dirTest/nihao/明天.icd"
"F:/dirTest/tayehao/nihao.icd"
"F:/dirTest/wobuhao.icd"

Method Two

Using the entryInfoList of the QDir class
insert image description hereis more powerful, and can specify the file name to be traversed, as well as the standard and sorting functions of the traverse. As for the relationship entryInfoListwith them , we generally use the former, because there are many convenient interfaces. Things to pay attention to 1. How to return all entries (including entries under subfolders), at least need to specify . It's just that the docs don't say it straight (at least I think). 2. The default parameter does not mean that nothing is listed. Instead it returns readable or writable or executable. Or a file or file directory entry matching any combination of these three. the codeentryListentryInfoListQFileInfoListQFileInfo

entryInfoListQDir::AllDirs
Filters filters = NoFilter
A default constructed QDir will not filter out files based on their permissions, so entryList() and entryInfoList() will return all files that are readable, writable, executable, or any combination of the three. This makes the default easy to write, and at the same time useful.

void Widget::getAllFiles(const QString &dirPath)
{
    
    
    QDir dir(dirPath);
    QFileInfoList list = dir.entryInfoList(QDir::Files | QDir::AllDirs | QDir::NoSymLinks | QDir::NoDotAndDotDot);
    foreach(QFileInfo fileInfo, list)
    {
    
    
        if(fileInfo.isDir())
        {
    
    
            getAllFiles(fileInfo.absoluteFilePath());
        }
        else
        {
    
    
            fileList.append(fileInfo.absoluteFilePath());
        }
    }
}

output

"F:/dirTest/nihao/qunimade/max.icd"
"F:/dirTest/nihao/明天.icd"
"F:/dirTest/tayehao/nihao.icd"
"F:/dirTest/wobuhao.icd"

Tips Insert picture description constructor parameters
here can support some simple regular expressions. For the above example (looking for a file that ends with ), you can also write it like this. the code
QDirIteratornameFilters.icd

   QString dirPath = "F:\\dirTest";
    QStringList list;
    QDirIterator it(dirPath, QStringList() << "*.icd", QDir::Files | QDir::NoSymLinks | QDir::NoDotAndDotDot, QDirIterator::Subdirectories);
    while(it.hasNext())
    {
    
    
        it.next();
        list.append(it.fileInfo().absoluteFilePath());
    }
    foreach(QString str, list)
    {
    
    
        qDebug() << str;
    }

output

"F:/dirTest/nihao/qunimade/max.icd"
"F:/dirTest/nihao/明天.icd"
"F:/dirTest/tayehao/nihao.icd"
"F:/dirTest/wobuhao.icd"

change into

QDirIterator it(dirPath, QStringList() << "*hao.icd", QDir::Files | QDir::NoSymLinks | QDir::NoDotAndDotDot, QDirIterator::Subdirectories);

output

"F:/dirTest/tayehao/nihao.icd"
"F:/dirTest/wobuhao.icd"

Guess you like

Origin blog.csdn.net/qq_40170041/article/details/131572971