Recursively Search Through Directories

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2008
Posts: 16
Reputation: launic is an unknown quantity at this point 
Solved Threads: 0
launic launic is offline Offline
Newbie Poster

Recursively Search Through Directories

 
0
  #1
Oct 6th, 2008
I'm trying to recursively search through directories and pull out any files or subdirectories.

This is the code I have:
  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
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 275
Reputation: dougy83 is on a distinguished road 
Solved Threads: 45
dougy83 dougy83 is offline Offline
Posting Whiz in Training

Re: Recursively Search Through Directories

 
-1
  #2
Oct 7th, 2008
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 ".
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,117
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 282
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Recursively Search Through Directories

 
0
  #3
Oct 7th, 2008
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.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 1
Reputation: zamp has a little shameless behaviour in the past 
Solved Threads: 0
zamp zamp is offline Offline
Newbie Poster

Re: Recursively Search Through Directories

 
-1
  #4
Mar 22nd, 2009
Usage:
  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
  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
  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. }
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC