Transversing the MS-Windows File System

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

Join Date: Aug 2005
Posts: 15,362
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1464
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Transversing the MS-Windows File System

 
0
  #1
Aug 24th, 2005
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.

  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. }
Reply With Quote Quick reply to this message  
Join Date: Feb 2002
Posts: 12,040
Reputation: cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light 
Solved Threads: 127
Administrator
Staff Writer
cscgal's Avatar
cscgal cscgal is offline Offline
The Queen of DaniWeb

Re: Transversing the MS-Windows File System

 
0
  #2
Nov 9th, 2005
Hmm ... I think something like this would be better suited to our Code Snippets section ... thanky very much for the contribution nonetheless!
Dani the Computer Science Gal
Follow my Twitter feed! twitter.com/daniweb
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