Qt实现遍历文件夹和文件目录

https://jingyan.baidu.com/article/9989c74673857df648ecfed2.html


采用递归和QDir实现文件夹下所有文件遍历的方法

#include <QDir>

bool FindFile(const QString &path)

{

    QDir dir(path);

    if (!dir.exists())

        return false;

    dir.setFilter(QDir::Dirs|QDir::Files);

    dir.setSorting(QDir::DirsFirst);

    QFileInfoList list = dir.entryInfoList();

    int i = 0;

    bool bIsDir;

    do

    {

        QFileInfo fileInfo = list.at(i);

        if (fileInfo.fileName()=="."|fileInfo.fileName()=="..")

        {

            i++;

            continue;

        }

        bIsDir = fileInfo.isDir();

        if (bIsDir)

        {

            nFiles++;                //对文件属性进行处理

            //fileInfo.size(),fileInfo.fileName(),fileInfo.path();

            FindFile(fileInfo.filePath());

        }

        else

        {

            nFiles++;

        }

        i++;

    }while (i<list.size());

}


猜你喜欢

转载自blog.csdn.net/chenyijun/article/details/80867166