954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Recursively Search Through Directories

I'm trying to recursively search through directories and pull out any files or subdirectories.

This is the code I have:

int readDir(string directory, vector<string> &fileList) 
//directory is the name of a direcotry 
//fileList is an empty vector

DIR *dir;
struct dirent *ent;

if((dir = opendir(director.c_str()) == NULL){
     cout<<"Invalid Directory"<<endl;
     exit(1);
}
while((ent = readdir(dir)) != NULL){
     fileList.push_back(string(ent->d_name));
}
closedir(dir);
return 0;
}

As of right now, it can read in a directory and list the files and subdirectories. I want it to now go through each subdirectory and get the files inside. Does anyone know how to go about making this a recursive function???

launic
Newbie Poster
16 posts since Sep 2008
Reputation Points: 10
Solved Threads: 0
 

How about you use code tags and post code that can be compiled.

There are easier ways to get file listings by using the OS list functions & _popen(). The output can be read using fread(). In windows: "dir /s/b ", in Linux: you could try "ls -R -l ".

dougy83
Posting Whiz in Training
275 posts since Jun 2007
Reputation Points: 85
Solved Threads: 45
 

When you read a file that's a directory, call the function again. And learn to use
CODE tags. There are at least 6 places on the site they are explained.

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

Usage:

DirTraveler traveler;
vector<string>foo;
traveler.travelDirectoryRecursive("bar", &foo);
for (int i=0; i<foo.size(); ++i)
    cout << foo[i].c_str() << endl;


DirTraveler.h

#ifndef DIRTRAVELER_H
#define DIRTRAVELER_H

#include <string>
#include <vector>

using namespace std;

/// Directory traveler (mostly used with zgui manager)

class DirTraveler
{
    public:
        DirTraveler();
        virtual ~DirTraveler();

        vector<string> travelDirectory(string directory);
        void travelDirectoryRecursive(string directory, vector<string> *fullList);
    protected:
    private:
};

#endif // DIRTRAVELER_H


DirTraveler.cpp

#include "DirTraveler.h"
#include <stdio.h>
#include <string.h>
#include <sstream>
#include <sys/types.h>
#include <dirent.h>
#include <vector>

using namespace std;

DirTraveler::DirTraveler()
{
    //ctor
}

DirTraveler::~DirTraveler()
{
    //dtor
}

vector<string> DirTraveler::travelDirectory(string directory)
{
    // travel thru a directory gathering all the file and directory naems
    vector<string> fileList;
    DIR *dir;
    struct dirent *ent;

    // open a directory
    if ((dir=opendir(directory.c_str())) != NULL)
    {
        while((ent=readdir(dir)) != NULL) // loop until the directory is traveled thru
        {
            // push directory or filename to the list
            fileList.push_back(ent->d_name);
        }
        // close up
        closedir(dir);
    }
    //return the filelust
    return fileList;
}

void DirTraveler::travelDirectoryRecursive(string directory, vector<string> *fullList)
{
    // get the "root" directory's directories
    vector<string> fileList = travelDirectory(directory);

    // loop thru the list
    for (vector<string>::iterator i=fileList.begin(); i!=fileList.end(); ++i)
    {
        // test for . and .. directories (this and back)
        if (strcmp((*i).c_str(), ".") &&
            strcmp((*i).c_str(), ".."))
        {
            // i use stringstream here, not string = foo; string.append(bar);
            stringstream fullname;
            fullname << directory << "/" << (*i);

            fullList->push_back(fullname.str());

            travelDirectoryRecursive(fullname.str(), fullList);
        }
    }
}
zamp
Newbie Poster
1 post since Mar 2009
Reputation Points: 3
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You