Hello i want to create a file crawler that lists all files in hard disk using directory functions in C i have been able to list files in one directory only and haven't been able to crawl through other directories and sub-directories this is what i have so far.

char workingDir[100];
    getcwd(workingDir, 100);
    printf("Working Dir \t%s\n", workingDir);
    //Change dirctory to etc
    chdir("/home/Madawar");
    getcwd(workingDir, 100);
    printf("Changed to \t%s\n", workingDir);
    //Dirent stuff
    DIR *directory = opendir(workingDir);
    struct dirent *dirs;
    printf("Directories in %s\n",workingDir);
   while((dirs = readdir(directory))!='\0')
   {
       printf("Dir Name: %s\n\tDir No: %d\nDir Type: %u\n",dirs->d_name,dirs->d_ino,dirs->d_type);
   }

Recommended Answers

All 4 Replies

Check this thread out -- it was written in c++ but could be easily ported to C. If you read the code you will discover that it uses recursion to crawl though all sub directories.

Hi,Dragon i don't know much of C++ but i guess i can deduce part of the code does

vector<string> LIST;

mean that the position of the next readdir() call in
the directory stream are stored in a list or array of some sort and should i use an array to store the next readdir() call in C code?

Ignore the vector part and concentrate on how it recognizes a new sub directory and makes a recursive call to process the files in it. In C language I would replace that vector with a linked list if you need to keep the list of all files in memory for some reason.

Cool Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.