0

How can I count the number of files and folders in a directory using C. I have no clue. I couldn't write single line code. I do not care about current directory . and the parent directory.. I have to give a pathname for example “C:/Users/me/Documents/Example” while I'm running the program.

And I need a output like " There are 2 folders and 4 files in this directory".

alongova
  • 35
  • 5
  • Welcome to Stack Overflow. Please read [the help pages](http://stackoverflow.com/help), take the SO [tour], read [ask], as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). Lastly please learn how to create a [mcve]. – Some programmer dude Jun 17 '20 at 10:36
  • Look at the man pages for `opendir`, `readdir`, and `closedir`. Although they might not be available on Windows. – Tom Karzes Jun 17 '20 at 10:42
  • And there must be several thousands of examples on how to enumerate directories and files on Windows, both using the POSIX compatibility functions already mentioned by @TomKarzes, but also using native Windows API functions. Just use your favorite search engine and you should find them. – Some programmer dude Jun 17 '20 at 10:52
  • On Windows, use the functions `FindFirstFile` and `FindNextFile`. – Paul Ogilvie Jun 17 '20 at 11:33

1 Answers1

0

for linux os

The code below shall list all files and sub-directories in dir_path (for current directory use dir_path = ".").

DESCRIPTION

This description qouted from man7.org link

struct dirent *readdir(DIR *dirp);

readdir()

The readdir() function returns a pointer to a dirent structure representing the next directory entry in the directory stream pointed to by dirp. It returns NULL on reaching the end of the directory stream or if an error occurred. for further details go to the link above for man7.org.

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

int main(void) 
{ 
   /* de is Pointer for directory entry */
    struct dirent *de;  
    const char* dir_path = "C:/Users/me/Documents/Example";
    /*opendir() returns a pointer of DIR type.*/  
    DIR *dr = opendir(dir_path); 

    if (dr == NULL)  /* opendir returns NULL if couldn't open directory */
    { 
        printf("Could not open current directory" ); 
        return 0; 
    } 

    while ((de = readdir(dr)) != NULL){
        printf("%s\n", de->d_name); 
    }
    closedir(dr);     
    return 0; 
}

for windows os

for windows use the header file: fileapi.h see microsoft docs here: fileapi.h

this question answered before in SO in the link below using the FindFirstFile, FindNextFile and FindClose functions.

please review the answer in the link: list directory in windows in C programming language

Adam
  • 2,820
  • 1
  • 13
  • 33
  • I checked everything you sent. And Probably everything on here.I found an answer in the link: https://stackoverflow.com/questions/1121383/counting-the-number-of-files-in-a-directory-using-c a program on the answer count everything. I wanna seperate them like files and folders. These are so complicated for me I'm kind of beginner on programming. Can anyone help me? – alongova Jun 17 '20 at 14:27
  • I also found a link https://www.gnu.org/software/libc/manual/html_node/Testing-File-Type.html. Can I use S_ISDIR and S_ISREG macros while I separate regular files and folders? Anyone wanna show me? I Really need help Once a time Feed Me please Dont Teach me how to fish. – alongova Jun 17 '20 at 14:28