简体   繁体   English

计算文件夹中的文件(包括子文件夹)

[英]Count File in a folder (include subfolder)

hi i use Qt and i want to know how much file are in a folder (include subfolder) i find some code in internet but all of them only count file in a folder and not its subfolder嗨,我使用 Qt,我想知道文件夹中有多少文件(包括子文件夹)我在互联网上找到了一些代码,但它们都只计算文件夹中的文件,而不是其子文件夹
like this codes喜欢这个代码

#include <stdio.h>
#include <dirent.h>

int main(int argc, char *argv[])
{
    if(argc != 2)
    {
        printf("Usage: ./count \"<path>\"\n");
        return 1;
    }

    struct dirent *de;
    DIR *dir = opendir(argv[1]);
    if(!dir)
    {
        printf("opendir() failed! Does it exist?\n");
        return 1;
    }

    unsigned long count=0;
        while(de = readdir(dir))
     {
          ++count;
     }

    closedir(dir);
    printf("%lu\n", count);

    return 0;
}

i find this code here , it only count file in a folder and not its subfoler在这里找到这个代码,它只计算文件夹中的文件,而不是它的子文件夹
anyone can help me任何人都可以帮助我
edit 1编辑 1
i test this code from this page, it working but it can edit for less cpu usage?我从这个页面测试了这段代码,它可以工作,但它可以编辑以减少 CPU 使用率?

The following code runs for Qt.以下代码为 Qt 运行。

QStringList nameFilters;
nameFilters << "*.jpg";
QDirIterator it(QDir::currentPath(), nameFilters, QDir::Files, QDirIterator::Subdirectories);
while (it.hasNext()) {
    qDebug() << it.next();
}

use nameFilters << "*";使用nameFilters << "*"; if you want to know all the files in the parent directory and its sub-directories.如果您想知道父目录及其子目录中的所有文件。

sumit_smk code is ok but its need a little edit for your case sumit_smk 代码没问题,但需要对您的情况稍作修改
here is the code you can use这是您可以使用的代码

int file = 0;
QDirIterator it("/Your_folder/", QDir::Files, QDirIterator::Subdirectories);
    while (it.hasNext())
    {
        if(it.next() > 0 )
        {
            file++;
        }
    }
    qDebug() << file;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM