Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Counting the number of files in a directory using C

Tags:

c

file

linux

How can I count the number of files in a directory using C on linux platform.

like image 758
penguru Avatar asked Jul 13 '09 18:07

penguru


People also ask

Is there a way to count the number of files in a folder?

Browse to the folder containing the files you want to count. Highlight one of the files in that folder and press the keyboard shortcut Ctrl + A to highlight all files and folders in that folder. In the Explorer status bar, you'll see how many files and folders are highlighted, as shown in the picture below.

How do I count the number of files in a Windows folder?

On the contextual menu, select Properties. Alternatively, select the folder and press the Alt + Enter keys on your keyboard. When the Properties window opens, Windows 10 automatically starts counting the files and folders inside the selected directory.


1 Answers

No guarantee that this code compiles, and it's really only compatible with Linux and the BSDs:

#include <dirent.h>  ...  int file_count = 0; DIR * dirp; struct dirent * entry;  dirp = opendir("path"); /* There should be error handling after this */ while ((entry = readdir(dirp)) != NULL) {     if (entry->d_type == DT_REG) { /* If the entry is a regular file */          file_count++;     } } closedir(dirp); 
like image 127
Michiel Buddingh Avatar answered Oct 13 '22 20:10

Michiel Buddingh