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

88 篇文章 0 订阅
5 篇文章 0 订阅
    今天整理数据库的时候发现,需要统计下不同分辨率的图片的数目的多少,本来想着按照图片的大小排个序,然后就可以人工统计,后面发现大小相同的图片,分辨可不一样相同,那就没办法人工统计了。没办法,只好用电脑来完成这种不用脑的体力活了。
    这个程序主要利用的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;
    }


}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值