User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 391,922 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,713 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser:
Oct 27th, 2006
Views: 3,558
I previously posted an MS-Windows example program about how to recursively search folders and create a list of their filename. This program is similar in concept but is for linux. It c++ and uses std::string and std::vector classes to hold the strings. When the program encounters a directory it will recursively call itself to process that directory. All the directory names are placed into a vector of vectors.

One improvement that needs to be made if using this program is to maintain the names of the directories along with the vector or filenames.

Happy programming.
c Syntax | 5 stars
  1. /* This program is not copywrite and is made public domain for anyone to use
  2. for any purpuse either private or commercial. The only restriction is that is that the source code shall not be sold for profit but it can be freely compiled and embedded into any program in binary form.
  3. */
  4.  
  5. #include <iostream>
  6. #include <vector>
  7. #include <string>
  8. #include <sys/types.h>
  9. #include <dirent.h>
  10.  
  11. using namespace std;
  12.  
  13. typedef vector<string> LIST;
  14. int seekdir(vector<LIST>& mylist, string path)
  15. {
  16.  
  17. DIR* dir = opendir(path.c_str());
  18. vector<string> lst;
  19. if(dir == 0)
  20. {
  21. cout << "Can not open directory '" << path << "'\n";
  22. return 1;
  23. }
  24. // cout << "directory '" << path << "' opened ok\n";
  25. struct dirent *d;
  26. while( (d = readdir(dir)) != 0)
  27. {
  28. if( d->d_type == DT_DIR && strcmp(d->d_name,".") != 0 &&
  29. strcmp(d->d_name,"..") != 0)
  30. {
  31. string p = path + "/" + d->d_name;
  32. seekdir(mylist,p);
  33.  
  34. }
  35. else if(d->d_type == DT_REG)
  36. {
  37. lst.push_back(d->d_name);
  38. }
  39.  
  40. }
  41. closedir(dir);
  42. mylist.push_back(lst);
  43. return 0;
  44. }
  45.  
  46. int main()
  47. {
  48. const char* home = getenv("HOME");
  49. vector<LIST> mylist;
  50. seekdir(mylist,home);
  51. int ndirs = mylist.size();
  52. cout << "ndirs: " << ndirs << "\n";
  53. int nfiles = 0;
  54. vector<LIST>::iterator it;
  55. for(it = mylist.begin(); it != mylist.end(); it++)
  56. {
  57. LIST& lst = *it;
  58. nfiles += lst.size();
  59. }
  60. cout << "nfiles: " << nfiles << "\n";
  61. return 0;
  62. }
Comments (Newest First)
majestic0110 | Veteran Poster | Apr 4th, 2008
very nice work!
flavour_of_bru | Newbie Poster | Jul 30th, 2007
Hi,

I am working on the same part of the logic and got stuck here. The above code will work for linux, but I am working on windows platform.

I have seen in the post that you already gave such an example in the previous post, I searched for it but could not find it.

Can you please send me the link to that post or else if you can help me with the same in windows, that would be really great.

Thanks!!
Osama Mehtab | Newbie Poster | Apr 13th, 2007
thanks

i will try understanding it
and'
will
ask u again
if got any query inthis program

venkal | Newbie Poster | Feb 28th, 2007
hi i m venki

I am have two os windows xp and red hat. In my college we are working in the linux terminal in windows 2000 professional by typing telnet 192.168.2.8 in Run . I tried the same in my system but it is not working. I have to switch form XP to linux ever time. Kindly how to over come the problem

Thanks
rajkumar veer | Newbie Poster | Feb 21st, 2007
hi i m raj.
i need c source code in windows for searching directory and file in disk..i hv some problem in my source code.

thanks
nanodano | Junior Poster in Training | Nov 1st, 2006
I like this snippet. I always find this kind of C++ code very useful.
Post Comment

Only community members can submit or comment on code snippets. You must register or log in to contribute.

DaniWeb Marketplace (Sponsored Links)
All times are GMT -4. The time now is 8:14 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC