Link to home
Start Free TrialLog in
Avatar of rockymagee
rockymagee

asked on

Recursive directory reader in C

I have this code so far, I know I need to add recursion to the equation:

If you run it you can see that I can get to all the files in the root of C:, but I
am having trouble recursing through to the subfolders .... and thier subfolders, etc...

I am not sure how to add the recursive flow to the opendir() and readdir() functions.

?????? readdir() moves to the next item in the directory ??????

/********** BEGIN OF CODE**********/
#include <sys/stat.h>
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>

   char *path;
   char file_name[4096];
   char *newDir;
   char newPath[100];
   DIR * directory;
   DIR * subDirectory;
   struct dirent *dir_result;
   struct stat stat_buf;
   int stat_result;

    main(){
   
      path = "C:\\";
      printf("%s\n", path);
   
      directory = opendir(path);
      dir_result = readdir(directory);
   
      while ( dir_result != NULL) {
         traverse();
         dir_result = readdir(directory);
      }    
      getchar();
   }

    traverse(){
      sprintf(file_name, "%s\%s", path, dir_result->d_name);
      stat_result = stat(file_name, &stat_buf);    
     
         if (S_ISDIR(stat_buf.st_mode)){
            printf("%s\n", file_name);
         
            newDir = dir_result->d_name;
            newPath[0] = '\0';
            strcat(newPath, path);
            strcat(newPath, newDir);
            strcat(newPath, "\\");
            printf("%s\n", newPath);
            subDirectory = opendir(newPath);
            readdir(subDirectory);
            // traverse();
         }
   }
/********** END OF CODE**********/

If I am way off the path to doing this correctly, suggestions would also be
very much welcome ....

TIA
Avatar of ozo
ozo
Flag of United States of America image

"\%" is not a standard escape sequence
Avatar of Raymun
Raymun

void read() {
    if( !readdir( directory )) {
        traverse();
        read();
    }
}

int main() {
    directory = opendir(path);
    read();
}
ASKER CERTIFIED SOLUTION
Avatar of Kent Olsen
Kent Olsen
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rockymagee

ASKER

I do not seem to be printing anything out ... do the changes you made have any effect on the main() ?

Also, it looks like we are trying to

subDirectory = opendir(newPath);

before we ever get it?

Thanks for your assistance :)
I just realized you needed that dir_result.

void read() {
    dir_result = readdir( directory );

    if( !dir_result) {
        traverse();
        read();
    }
}

int main() {
    path = "C:\\";
    printf("%s\n", path);
    directory = opendir(path);
    read();
    getchar();
}
Raymun,

I am not sure how the code you have given me fits into what I have already coded?

TIA
#include <sys/stat.h>
// rest of includes...

char *path;
// rest of variables

void read() {
    dir_result = readdir( directory );

    if( !dir_result) {
        traverse();
        read();
    }
}

int main() {
    path = "C:\\";
    printf("%s\n", path);
    directory = opendir(path);
    read();
    getchar();
}

traverse() {
   // traverse body
}
Raymun .... getting the following errors when I try the
   
   read()
   main()
   traverse()

order

::::


`void read()':
`traverse' undeclared (first use this function)
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Raymun ... first off I really appreciate the help !

It is throwing an error on the strcat function now?
Are you able to compile this to see the same error?

Here is the code do far:

#include <sys/stat.h>
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>

   char *path = "C:\\";;
   char file_name[4096];
   char *newDir;
   char newPath[100];
   DIR * directory;
   DIR * subDirectory;
   struct dirent *dir_result;
   struct stat stat_buf;
   int stat_result;
 
void traverse(){
  sprintf(file_name, "%s\%s", path, dir_result->d_name);
  stat_result = stat(file_name, &stat_buf);    
       if (S_ISDIR(stat_buf.st_mode)){
        printf("%s\n", file_name);
     
        newDir = dir_result->d_name;
        newPath[0] = '\0';
        strcat(newPath, path);
        strcat(newPath, newDir);
        strcat(newPath, "\\");
        //printf("%s\n", newPath);
        path = newPath;
        //printf("%s\n", path);
        //subDirectory = opendir(path);
        //readdir(subDirectory);
        //traverse();
     }
}

void read() {
  dir_result = readdir( directory );
       if( !dir_result) {
           traverse();
           read();
       }
}

int main() {
  directory = opendir(path);
  printf("%s\n", path);
  read();
  getchar();
}
I don't have a compiler installed so you need to tell me what the errors are. If you getting an error about it not recognizing strcat then you also need

#include <string>