现在的位置: 首页 > 综合 > 正文

遍历一个目录下的所有文件

2018年04月13日 ⁄ 综合 ⁄ 共 1133字 ⁄ 字号 评论关闭
    今天整理数据库的时候发现,需要统计下不同分辨率的图片的数目的多少,本来想着按照图片的大小排个序,然后就可以人工统计,后面发现大小相同的图片,分辨可不一样相同,那就没办法人工统计了。没办法,只好用电脑来完成这种不用脑的体力活了。

    这个程序主要利用的QT的QDir类来实现的(QT真是好强大)。
    下面来看代码。
这个是获取一个文件夹下面的文件名字的函数
#include <QDir>
#include <iostream>
/**
 * @brief GetFilesInDirectory This function get the
 * @param Directory
 * @return A list of the names of all the files, or An empty list if the
 *          directory is unreadable, does not exist,or dose not contain
 *          any files.
 *
 * @author  sheng
 * @version 1.0.0
 * @date    2014-04-03
 *
 * @history   <author>      <date>         <description>
 *             sheng      2014-04-03         build the function
 *
 */
QStringList GetFilesInDirectory(const QString &Directory)
{
    // set the directory
    QDir Dir(Directory);

    // set the filter and sorting rules.
    Dir.setFilter(QDir::Files | QDir::NoSymLinks);
    Dir.setSorting(QDir::Size | QDir::Name);

    // return the FileList in the directory
    return Dir.entryList();
}


下面是测试程序
#include <QDir>
#include <Functions.h>
#include <iostream>

void Test_GetFilesInDirectory()
{


    QStringList FileList =
            GetFilesInDirectory("E:/");

    std::cout << "The size of the FileList is " << FileList.size()
              << std::endl;

    for (int i = 0; i < FileList.size(); i++)
    {
        QString FileName = FileList.at(i);
        std::cout << "No." << (i + 1) << "Picture's name is ";
        std::cout << FileName.toLocal8Bit().constData() << std::endl;
    }


}

抱歉!评论已关闭.