S
S
Sergo Zar2021-12-12 00:38:53
Qt
Sergo Zar, 2021-12-12 00:38:53

How to get files from folders and subfolders?

I am trying to get a list of all files in a folder and subfolders. I try like this:

QList <QString> files;

void SearchFiles::scanDir(QDir dir){
    if(!dir.exists()){
        isFind = false;
        return;
    }
    QFileInfoList listDir = dir.entryInfoList();
    QFileInfo file;
    foreach(QFileInfo finfo, listDir){
        if(finfo.fileName() != "." || finfo.fileName() != ".."){
            file.setFile(finfo.fileName());
            if(file.isDir())
                scanDir(file.absoluteDir());
            else
                files.push_back(file.absoluteFilePath());
        }
    }
}

the application crashes. What am I doing wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergo Zar, 2021-12-12
@Sergomen

It turned out to be easier than I thought:

QList <QString> files;

void SearchFiles::scanDir(QDir dir){
    if(!dir.exists()){
        isFind = false;
        return;
    }
     QDirIterator it(dir, QDirIterator::Subdirectories);
    QFileInfo finfo;
    while (it.hasNext()) {
        info.setFile(it.next());
        if(finfo.fileName() != "." || finfo.fileName() != ".."){
            files.push_back(finfo.absoluteFilePath());
        }
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question