•
•
•
•
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
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().
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.
#pragma warning(disable: 4786) #include <io.h> #include <string> #include <vector> #include <list> #include <iostream> using namespace std; // structure to hold a directory and all its filenames. struct FILELIST { string path; vector<string> theList; }; void TransverseDirectory(string path, list<FILELIST>& theList) { struct _finddatai64_t data; string fname = path + "\\*.*"; long h = _findfirsti64(fname.c_str(),&data); if(h >= 0) { FILELIST thisList; theList.push_back(thisList); list<FILELIST>::iterator it = theList.end(); it--; (*it).path = path; do { if( (data.attrib & _A_SUBDIR) ) { // make sure we skip "." and "..". Have to use strcmp here because // some file names can start with a dot, so just testing for the // first dot is not suffient. if( strcmp(data.name,".") != 0 &&strcmp(data.name,"..") != 0) { // We found a sub-directory, so get the files in it too fname = path + "\\" + data.name; // recurrsion here! TransverseDirectory(fname,theList); } } else { // this is just a normal filename. So just add it to our vector (*it).theList.push_back(data.name); } }while( _findnexti64(h,&data) == 0); _findclose(h); } } int main(int argc, char* argv[]) { list<FILELIST> MyList; string path; cout << "Enter starting path ... "; getline(cin,path); TransverseDirectory(path,MyList); list<FILELIST>::iterator it; for(it = MyList.begin(); it != MyList.end(); it++) { vector<string>::iterator its; for(its = (*it).theList.begin(); its != (*it).theList.end(); its++) { cout << (*it).path + "\\" + (*its) << endl; } } cin.ignore(); return 0; }
Comments (Newest First)
Cybulski | C++ wannabe | May 16th, 2008
•
•
•
•
I get this error:
I pass data.name to string before calling strcmp(), but I wonder how it works for you.
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
•
•
•
•
DaniWeb Marketplace (Sponsored Links)