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 425,870 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 2,460 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: Programming Forums
Aug 24th, 2005
Views: 2,414
short program that will build a stl::list of directory names and all the files they contain. recursively calls itself when a new directory is encountered. This has been compiled with both VC++ 6.0 and Dev-Shed compiles on MS-Windows XP Pro. It will not work on *nix or probably MAC computers.

Illustrates simple use of std::list, std::vector, std::string, std::cin and std::cout. as well as intention of the program using _findfirsti64() and _findnexti64().
Last edited : Jun 30th, 2006.
cplusplus Syntax | 4 stars
  1. #pragma warning(disable: 4786)
  2. #include <io.h>
  3. #include <string>
  4. #include <vector>
  5. #include <list>
  6. #include <iostream>
  7. using namespace std;
  8.  
  9.  
  10. // structure to hold a directory and all its filenames.
  11. struct FILELIST
  12. {
  13. string path;
  14. vector<string> theList;
  15. };
  16.  
  17.  
  18. void TransverseDirectory(string path, list<FILELIST>& theList)
  19. {
  20. struct _finddatai64_t data;
  21. string fname = path + "\\*.*";
  22. long h = _findfirsti64(fname.c_str(),&data);
  23. if(h >= 0)
  24. {
  25. FILELIST thisList;
  26. theList.push_back(thisList);
  27. list<FILELIST>::iterator it = theList.end();
  28. it--;
  29.  
  30. (*it).path = path;
  31. do {
  32. if( (data.attrib & _A_SUBDIR) )
  33. {
  34. // make sure we skip "." and "..". Have to use strcmp here because
  35. // some file names can start with a dot, so just testing for the
  36. // first dot is not suffient.
  37. if( strcmp(data.name,".") != 0 &&strcmp(data.name,"..") != 0)
  38. {
  39. // We found a sub-directory, so get the files in it too
  40. fname = path + "\\" + data.name;
  41. // recurrsion here!
  42. TransverseDirectory(fname,theList);
  43. }
  44.  
  45. }
  46. else
  47. {
  48. // this is just a normal filename. So just add it to our vector
  49. (*it).theList.push_back(data.name);
  50.  
  51. }
  52. }while( _findnexti64(h,&data) == 0);
  53. _findclose(h);
  54.  
  55. }
  56.  
  57. }
  58.  
  59. int main(int argc, char* argv[])
  60. {
  61. list<FILELIST> MyList;
  62. string path;
  63. cout << "Enter starting path ... ";
  64. getline(cin,path);
  65. TransverseDirectory(path,MyList);
  66. list<FILELIST>::iterator it;
  67. for(it = MyList.begin(); it != MyList.end(); it++)
  68. {
  69. vector<string>::iterator its;
  70. for(its = (*it).theList.begin(); its != (*it).theList.end(); its++)
  71. {
  72. cout << (*it).path + "\\" + (*its) << endl;
  73. }
  74.  
  75. }
  76. cin.ignore();
  77. return 0;
  78. }
Comments (Newest First)
Cybulski | C++ wannabe | May 16th, 2008
I get this error:
error C2664: 'strcmp' : cannot convert parameter 1 from '__finddata64_t' to 'const char *'

I pass data.name to string before calling strcmp(), but I wonder how it works for you.
vegaseat | Kickbutt Moderator | Mar 24th, 2006
This is a C++ program and should be moved to the cplusplus snippets!
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 5:54 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC