问题
I am new in c ++ . could someone please give me some code of how to get all directories and all it's subdirectories RECURSIVELY in LINUX. i haven't found anything on internet that could help me ( or code that works.) I need to get all files withing folder and it;s subfolder.
IN UBUNTU I don't have getfiles, directories...
回答1:
Try this on Linux:
#include <iostream>
#include <string>
#include <dirent.h>
void ProcessDirectory(std::string directory);
void ProcessFile(std::string file);
void ProcessEntity(struct dirent* entity);
std::string path = "/path/to/directory/";
int main()
{
std::string directory = "theDirectoryYouWant";
ProcessDirectory(directory);
return 0;
}
void ProcessDirectory(std::string directory)
{
std::string dirToOpen = path + directory;
auto dir = opendir(dirToOpen.c_str());
//set the new path for the content of the directory
path = dirToOpen + "/";
std::cout << "Process directory: " << dirToOpen.c_str() << std::endl;
if(NULL == dir)
{
std::cout << "could not open directory: " << dirToOpen.c_str() << std::endl;
return;
}
auto entity = readdir(dir);
while(entity != NULL)
{
ProcessEntity(entity);
entity = readdir(dir);
}
//we finished with the directory so remove it from the path
path.resize(path.length() - 1 - directory.length());
closedir(dir);
}
void ProcessEntity(struct dirent* entity)
{
//find entity type
if(entity->d_type == DT_DIR)
{//it's an direcotry
//don't process the '..' and the '.' directories
if(entity->d_name[0] == '.')
{
return;
}
//it's an directory so process it
ProcessDirectory(std::string(entity->d_name));
return;
}
if(entity->d_type == DT_REG)
{//regular file
ProcessFile(std::string(entity->d_name));
return;
}
//there are some other types
//read here http://linux.die.net/man/3/readdir
std::cout << "Not a file or directory: " << entity->d_name << std::endl;
}
void ProcessFile(std::string file)
{
std::cout << "Process file : " << fileToOpen.c_str() << std::endl;
//if you want to do something with the file add your code here
}
回答2:
Windows: http://msdn.microsoft.com/en-us/library/windows/desktop/aa365200(v=vs.85).aspx
Unix/Linux: http://www.linuxquestions.org/questions/programming-9/c-list-files-in-directory-379323
Recursively apply the same algorithm you applied to the top-level directory.
回答3:
Recursion is unnecessary. There is a facility on Linux to do this iteratively.
#include <ftw.h>
int ftw(const char *dirpath,
int (*fn) (const char *fpath, const struct stat *sb,
int typeflag),
int nopenfd);
The ftw() function calls the supplied callback for every file and directory in the given tree.
回答4:
In addition of Dmitri's answer, you might be interested in using the nftw library function which "does the recursion for you"
回答5:
Use nftw. It provides various options to fine-tune the directory traversal.
That page also shows an example.
回答6:
The answers on listing directories are only the first part of the answer but i didnt see anyone answering the recursive part. To recursively do anything you have to create a subroutine that "calls itself" -- note that you should take care when dealing with symbolic links especially in cases like /exports when using nfs, that could lead to circular recursion and lock you in an infinte loop! Basically:
this isn't real code, its pseudo-code to try to help you get a better idea of how the recursion works without confusing you with language this idea can be applied in any language that has some sort of call-return mechanism which these days is just about every language i can think of
// PSEUDO-CODE
stiring entriesarray[] myfunc(string basedir)
{
string results[] = getfilesandfolders(basedir) // you might want to specify a filter
for each string entry in results
{
// if you want only files you'll need to test for if entry is a file
entriesarray.add(entry)
if (entry is a directory && entries is not a symbolic link)
{
string moreentriesarray[] = myfunc(entry)
entriesarray.join(moreentriesarray)
}
}
return entriesarray[]
}
notice how if the entries do not contain any real directories, the function doesn't call itself? this is important because this is how infinite recursion is avoided. Be warned however you might want to make it so this operation can be cancelled, larger filesystems these days can take a lot of time to process. The way I usually do it is to start another thread and do the search in the background, and have the background thread check for a cancellation flag in case the user wants to stop the operation, and so it can post information about how much time is left, percentage complete, etc ,etc. its a bit crude but this should get anyone interested in this type of thing going in the right direction. And remember to ALWAYS properly check for errors and place exception handling, this is something that I see new programmers skip ALL the time.
来源:https://stackoverflow.com/questions/9138866/c-list-all-directories-and-subdirectories-within-in-linux