944,069 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 7865
  • C++ RSS
Aug 24th, 2005
0

Transversing the MS-Windows File System

Expand Post »
This is a simple example of how to use recursion to get a list of all the directories (folders) and file names within them. It also illustrates simple use if std::list, std::vector, std::string, std::cin and std::cout. It was compiled and tested with both VC++ 6.0 and Dev-C++ compilers on XP Pro.

C++ Syntax (Toggle Plain Text)
  1. // The next two lines are for VC++ 6.0 compiler. You should
  2. // delete them if you compile it with a different compiler.
  3. #include "stdafx.h"
  4. #pragma warning(disable: 4786)
  5. #include <io.h>
  6. #include <string>
  7. #include <vector>
  8. #include <list>
  9. #include <iostream>
  10. using namespace std;
  11.  
  12.  
  13. // structure to hold a directory and all its filenames.
  14. struct FILELIST
  15. {
  16. string path;
  17. vector<string> theList;
  18. };
  19.  
  20.  
  21. void TransverseDirectory(string path, list<FILELIST>& theList)
  22. {
  23. struct _finddatai64_t data;
  24. // First create the filename that will be use to initialize the find.
  25. // "*.*" are wild card characters that tells the find function to return a
  26. // list of all the files and directories. You can limit this if you wish
  27. // to just file with specific extensions, for example "*.txt". If you do that
  28. // then finder will not return any directory names.
  29. string fname = path + "\\*.*";
  30. // start the finder -- on error _findfirsti64() will return -1, otherwise if no
  31. // error it returns a handle greater than -1.
  32. long h = _findfirsti64(fname.c_str(),&data);
  33. if(h >= 0)
  34. {
  35. FILELIST thisList;
  36. // add empty FILELIST structure to the linked list argument
  37. theList.push_back(thisList);
  38. // get pointer to the FILELIST just added to the linked list above.
  39. list<FILELIST>::iterator it = theList.end();
  40. it--;
  41. // set current path
  42. (*it).path = path;
  43. do {
  44. if( (data.attrib & _A_SUBDIR) )
  45. {
  46. // make sure we skip "." and "..". Have to use strcmp here because
  47. // some file names can start with a dot, so just testing for the
  48. // first dot is not suffient.
  49. if( strcmp(data.name,".") != 0 &&strcmp(data.name,"..") != 0)
  50. {
  51. // We found a sub-directory, so get the files in it too
  52. fname = path + "\\" + data.name;
  53. // recursion here!
  54. TransverseDirectory(fname,theList);
  55. }
  56.  
  57. }
  58. else
  59. {
  60. // this is just a normal filename. So just add it to our vector
  61. (*it).theList.push_back(data.name);
  62.  
  63. }
  64. }while( _findnexti64(h,&data) == 0);
  65. // close the find handle.
  66. _findclose(h);
  67.  
  68. }
  69.  
  70. }
  71.  
  72. int main(int argc, char* argv[])
  73. {
  74. list<FILELIST> MyList;
  75. string path;
  76. cout << "Enter starting path ... ";
  77. getline(cin,path);
  78. TransverseDirectory(path,MyList);
  79. list<FILELIST>::iterator it;
  80. // now just display all the files
  81. for(it = MyList.begin(); it != MyList.end(); it++)
  82. {
  83. vector<string>::iterator its;
  84. for(its = (*it).theList.begin(); its != (*it).theList.end(); its++)
  85. {
  86. cout << (*it).path + "\\" + (*its) << endl;
  87. }
  88.  
  89. }
  90. cin.ignore();
  91. return 0;
  92. }
Similar Threads
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is online now Online
21,957 posts
since Aug 2005
Nov 9th, 2005
0

Re: Transversing the MS-Windows File System

Hmm ... I think something like this would be better suited to our Code Snippets section ... thanky very much for the contribution nonetheless!
Administrator
Staff Writer
Reputation Points: 1422
Solved Threads: 162
The Queen of DaniWeb
cscgal is offline Offline
13,646 posts
since Feb 2002

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: Handling Text from a multi-line edit box (win32api with c++)
Next Thread in C++ Forum Timeline: another question





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


Follow us on Twitter


© 2011 DaniWeb® LLC