943,636 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 4878
  • C++ RSS
Oct 6th, 2008
0

Recursively Search Through Directories

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

This is the code I have:
C++ Syntax (Toggle Plain Text)
  1. int readDir(string directory, vector<string> &fileList)
  2. //directory is the name of a direcotry
  3. //fileList is an empty vector
  4.  
  5. DIR *dir;
  6. struct dirent *ent;
  7.  
  8. if((dir = opendir(director.c_str()) == NULL){
  9. cout<<"Invalid Directory"<<endl;
  10. exit(1);
  11. }
  12. while((ent = readdir(dir)) != NULL){
  13. fileList.push_back(string(ent->d_name));
  14. }
  15. closedir(dir);
  16. return 0;
  17. }
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???
Last edited by Narue; Oct 7th, 2008 at 12:47 pm. Reason: added code tags
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
launic is offline Offline
16 posts
since Sep 2008
Oct 7th, 2008
-1

Re: Recursively Search Through Directories

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 ".
Reputation Points: 85
Solved Threads: 45
Posting Whiz in Training
dougy83 is offline Offline
275 posts
since Jun 2007
Oct 7th, 2008
0

Re: Recursively Search Through Directories

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.
Moderator
Reputation Points: 3278
Solved Threads: 890
Posting Sage
WaltP is offline Offline
7,717 posts
since May 2006
Mar 22nd, 2009
-1

Re: Recursively Search Through Directories

Usage:
c++ Syntax (Toggle Plain Text)
  1. DirTraveler traveler;
  2. vector<string>foo;
  3. traveler.travelDirectoryRecursive("bar", &foo);
  4. for (int i=0; i<foo.size(); ++i)
  5. cout << foo[i].c_str() << endl;

DirTraveler.h
c++ Syntax (Toggle Plain Text)
  1. #ifndef DIRTRAVELER_H
  2. #define DIRTRAVELER_H
  3.  
  4. #include <string>
  5. #include <vector>
  6.  
  7. using namespace std;
  8.  
  9. /// Directory traveler (mostly used with zgui manager)
  10.  
  11. class DirTraveler
  12. {
  13. public:
  14. DirTraveler();
  15. virtual ~DirTraveler();
  16.  
  17. vector<string> travelDirectory(string directory);
  18. void travelDirectoryRecursive(string directory, vector<string> *fullList);
  19. protected:
  20. private:
  21. };
  22.  
  23. #endif // DIRTRAVELER_H

DirTraveler.cpp
c++ Syntax (Toggle Plain Text)
  1. #include "DirTraveler.h"
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <sstream>
  5. #include <sys/types.h>
  6. #include <dirent.h>
  7. #include <vector>
  8.  
  9. using namespace std;
  10.  
  11. DirTraveler::DirTraveler()
  12. {
  13. //ctor
  14. }
  15.  
  16. DirTraveler::~DirTraveler()
  17. {
  18. //dtor
  19. }
  20.  
  21. vector<string> DirTraveler::travelDirectory(string directory)
  22. {
  23. // travel thru a directory gathering all the file and directory naems
  24. vector<string> fileList;
  25. DIR *dir;
  26. struct dirent *ent;
  27.  
  28. // open a directory
  29. if ((dir=opendir(directory.c_str())) != NULL)
  30. {
  31. while((ent=readdir(dir)) != NULL) // loop until the directory is traveled thru
  32. {
  33. // push directory or filename to the list
  34. fileList.push_back(ent->d_name);
  35. }
  36. // close up
  37. closedir(dir);
  38. }
  39. //return the filelust
  40. return fileList;
  41. }
  42.  
  43. void DirTraveler::travelDirectoryRecursive(string directory, vector<string> *fullList)
  44. {
  45. // get the "root" directory's directories
  46. vector<string> fileList = travelDirectory(directory);
  47.  
  48. // loop thru the list
  49. for (vector<string>::iterator i=fileList.begin(); i!=fileList.end(); ++i)
  50. {
  51. // test for . and .. directories (this and back)
  52. if (strcmp((*i).c_str(), ".") &&
  53. strcmp((*i).c_str(), ".."))
  54. {
  55. // i use stringstream here, not string = foo; string.append(bar);
  56. stringstream fullname;
  57. fullname << directory << "/" << (*i);
  58.  
  59. fullList->push_back(fullname.str());
  60.  
  61. travelDirectoryRecursive(fullname.str(), fullList);
  62. }
  63. }
  64. }
Reputation Points: 3
Solved Threads: 0
Newbie Poster
zamp is offline Offline
1 posts
since Mar 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Parity Check Matrix
Next Thread in C++ Forum Timeline: problem with calling inheritance class objects using vector





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC