0

I am trying to count numbers of files and subfolders in given path. I did some research but people did it mostly for Linux but I need to do it in Windows. This is my code:

#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
void listFiles(const char *path);
int main()
{   
    char path[100];
    printf("Enter path to list files: ");
    scanf("%s", path);
    listFiles(path);
    return 0;
}
void listFiles(const char *path)
{
    struct dirent *dp;
    DIR *dir = opendir(path);
    int fileCount = 0;
    int dirCount = 0;
    int i, n;

    if (!dir) 
        return; 

    for(i = 1; i < n; i++){
    while ((dp = readdir(dir)) != NULL)
    {
       if(dir->d_type == DT_REG){
                fileCount++;
          }
          if(dir->d_type == DT_DIR)
                dirCount++;
        }
         printf("%s: file count is: %d and dir count is: %d\n",path, fileCount, dirCount);
    }    
    closedir(dir);
}

And this is errors I couldn't fix:

28  14      [Error] 'DIR' has no member named 'd_type'
28  26      [Error] 'DT_REG' undeclared (first use in this function)
28  26      [Note] each undeclared identifier is reported only once for each function it appears in
31  17      [Error] 'DIR' has no member named 'd_type'
31  29      [Error] 'DT_DIR' undeclared (first use in this function)
28          recipe for target '2.o' failed

Can you please help me how to fix it. I saw something about _BSD_SOURCE but I don't know if it's Linux or Windows.

Eren sert
  • 3
  • 3
  • Does this answer your question? [Counting number of files and folders in directory](https://stackoverflow.com/questions/62426882/counting-number-of-files-and-folders-in-directory) – Adam Jun 19 '20 at 12:42
  • "I am trying to count numbers of files and subfolders in given path." Hmm, no. Actually I think you are trying to compile code which you do not know anything about in an environment it was not meant for. Please explain where you got that source. – Yunnosch Jun 19 '20 at 12:43
  • its duplicate. this question asked and answered many times in SO. see this link. [list dir](https://stackoverflow.com/questions/62426882/counting-number-of-files-and-folders-in-directory/62427213#62427213). I have answered this question there. – Adam Jun 19 '20 at 12:43
  • I found this code from https://codeforwin.org/2018/03/c-program-to-list-all-files-in-a-directory-recursively.html It was for listing, I am tring to adapt it for counting not listing. @yunnosch – Eren sert Jun 19 '20 at 12:54
  • No man, I couldn't understand how to adapt it to count files and subfolders, the link you send is for listing @adam – Eren sert Jun 19 '20 at 13:01
  • 1
    listing a directory == to plot and to count the directory components – Adam Jun 19 '20 at 13:03
  • OK. It's listing now, how will it count files and folders? I did so many search but found nothing. Really desperate. – Eren sert Jun 19 '20 at 14:07
  • See example in answer, it shows functions for Windows to find files and directories, determine what it found, and shows first level counting. You will have to adapt to traverse through the levels to get a complete count. – ryyker Jun 19 '20 at 14:42

1 Answers1

0

Your title question indicates the need to do what you are doing on Windows, but some of the code you posted ( eg readdir() ) require POSIX compliance. It will not work on Windows.

Windows functions that can be used to count files & directories include the following:

FindFirstFile

FindNextFile

GetFileAttributes

Here is a basic frame work to get you started. It does compile, but will require adaptation before it will work for you. Your adaptation will require traversing down into any directories that are found, to search for more files/sub-directories, then back up to the top level. You can use the attributes, as shown, to make decisions on whether the found file is either a file or a directory, and increment the appropriate counter to keep track of each: (This example only counts first level directories and files.)

Add #include files as appropriate for your environment, which will have to include #include <windows.h> among others.

#include <windows.h>
//include other files as needed for your system

int main(int argc, char *argv[])
{
    FILE *out;
    int fileCount = 0;
    int dirCount = 0;


    DWORD attribute;

    WIN32_FIND_DATA file;
    HANDLE fhandle;

    out = fopen("file_list.txt", "w");
    if(out == NULL){
        printf( "Error creating file\n");
        return 0;
    }
    // Place your own starting point here.  
    fhandle = FindFirstFile("C:\\play\\*", &file);
    if(fhandle == INVALID_HANDLE_VALUE){
        printf("Invalid File Handle.\n");
        return 0;
    } 

    while(FindNextFile(fhandle, &file) != 0){
        attribute = GetFileAttributes(file.cFileName);
        switch(attribute) {
            case FILE_ATTRIBUTE_DIRECTORY:
                dirCount++;
                fprintf(out, "DIR - %s\n", file.cFileName);
                break;
            default:
                fileCount++;
                fprintf(out, "FILE - %s\n", file.cFileName);
                break;
        }                                                
    }
    fprintf(out, "Directories: %d\nFiles      :%d\n", dirCount, fileCount);
    fclose(out);

    return 0;
}
ryyker
  • 22,849
  • 3
  • 43
  • 87