2

Possible Duplicate:
count number of files with a given extension in a directory - C++?

How to get the number of files in the specific folder using c or c++ function? Is there any c library function that could get the number of files in a given directory?

jww
  • 97,681
  • 90
  • 411
  • 885
injoy
  • 3,993
  • 10
  • 40
  • 69
  • 10
    Did you even try to google this? – Kiril Kirov Aug 22 '12 at 14:54
  • Could do a system call to ls (or dir), split the output and count the elements – brthornbury Aug 22 '12 at 14:57
  • 2
    Unix/Linux (POSIX) or Windows? There's no single function to count files in POSIX; you can read the directory entries (`opendir()`, `readdir()`, `closedir()`), and check that they are files (`stat()` or `lstat()`) and do your own counting of the ones you want. (Do devices count as files? Symlinks? Sockets?) For recursive operations, use `nftw()` — 'new' file tree walk (as opposed to `ftw()` or 'old' file tree walk). – Jonathan Leffler Aug 22 '12 at 14:58
  • Here is a link to the similar question. http://stackoverflow.com/questions/1935274/count-number-of-files-with-a-given-extension-in-a-directory-c – andre Aug 22 '12 at 15:39
  • [Counting the number of files in a directory using C](https://stackoverflow.com/q/1121383/608639). – jww Dec 06 '18 at 13:25

4 Answers4

6

Here is a working example of opendir/readdir/closedir use (no recursion here):

void listdir(char *dir) {
  struct dirent *dp;
  DIR *fd;

  if ((fd = opendir(dir)) == NULL) {
    fprintf(stderr, "listdir: can't open %s\n", dir);
    return;
  }
  while ((dp = readdir(fd)) != NULL) {
  if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
    continue;    /* skip self and parent */
  printf("%s/%s\n", dir, dp->d_name);
  }
  closedir(fd);
}
Stephane Rouberol
  • 4,286
  • 19
  • 18
3

I don't think there is any standard method of listing the files in a directory. I remember when I had to do this before, I ended up using Boost Filesystem.

Antimony
  • 37,781
  • 10
  • 100
  • 107
3

With boost::filesystem it could look like this:

#include <iostream>
#include <algorithm>
#include <iterator>
#include <boost/filesystem.hpp>
#include <boost/iterator/filter_iterator.hpp>
namespace fs = boost::filesystem;

int main()
{
    fs::path p("D:/mingw/include");
    fs::directory_iterator dir_first(p), dir_last;

    auto pred = [](const fs::directory_entry& p)
    {
        return fs::is_regular_file(p);
    };

    std::cout <<
        std::distance(boost::make_filter_iterator(pred, dir_first, dir_last),
                      boost::make_filter_iterator(pred, dir_last, dir_last));
}
jrok
  • 54,456
  • 9
  • 109
  • 141
0

You need to open the directory with opendir(), and loop through the whole directory using readdir()... and count how many times you do.

Remember that '.' and '..' are special entries and don't count.

If you need to count files only and no directories, you will have to check explicitly in the dir struct (man stat).

If you need to have the number of files in the folder and its subfolders, then you will have to recurse ("walk") inside the directories - and maybe, depending on the platform, checking for symlinks.

LSerni
  • 55,617
  • 10
  • 65
  • 107
  • 1
    This is a POSIX-oriented answer (the interfaces to use are different on Windows). For recursive operations, use [`nftw()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/nftw.html). – Jonathan Leffler Aug 22 '12 at 15:03