当前位置:网站首页>QT reads all files under the path or files of the specified type (including recursion, judging whether it is empty and creating the path)

QT reads all files under the path or files of the specified type (including recursion, judging whether it is empty and creating the path)

2022-04-23 08:19:00 Ott_ Glodon

// Traverse to get all files under the folder

void Dialog::GetAllFiles(const QString path, QFileInfoList &fileInfoList)
{
    
    QDir dir(path);
    foreach(QFileInfo info,dir.entryInfoList(QDir::NoDotAndDotDot | QDir::Dirs))
    {
    
        //qDebug()<<"dir:" << info.filePath();
        GetAllFiles(info.filePath(),fileInfoList);
    }

    foreach(QFileInfo info,dir.entryInfoList(QDir::Files))
    {
    
        fileInfoList.append(info);
        qDebug()<< " file :" << info.filePath();
    }
}

// Get the file of the specified type under the current folder ( Non recursive )

void Dialog::GetDirFiles(const QString dirPath)
{
    
    QDir *dir = new QDir(dirPath);

    //  Determine if the path exists , Create if it does not exist 
    if(!dir->exists(dirPath))
    {
    
        //  Create path file , It can be multi-level 
        dir->mkpath(dirPath);
    }

    //  Determine whether the folder content is empty 
    QFileInfoList fileInfoList;
    GetAllFiles(dirPath, fileInfoList);
    if(fileInfoList.size() == 0)
    {
    
        QMessageBox::information(this," Tips "," There are no files in the file path !");
        return;
    }

//  Judge whether the file exists 
// QFile fileName(filePath);
// if(!fileName.exists())
// {
    
// return true;
// }

    QStringList filter;
    QList<QFileInfo> *fileInfo = new QList<QFileInfo>(dir->entryInfoList(filter));

    for(int i = 0; i < fileInfo->count(); i++)
    {
    
        if(fileInfo->at(i).fileName().split(".").back() != "cpp")
        {
    
            continue;
        }

        qDebug()<< fileInfo->at(i).filePath();
        qDebug()<< fileInfo->at(i).fileName();
    }
}

// Get all folders under the current folder ( Non recursive )

void Dialog::GetAllDirs(const QString path)
{
    
    QDir dir(path);
    foreach(QFileInfo info,dir.entryInfoList(QDir::NoDotAndDotDot | QDir::Dirs))
    {
    
        qDebug()<<"path:" << info.filePath();
        qDebug()<<"name:" << info.fileName();
    }
}

版权声明
本文为[Ott_ Glodon]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230714487330.html