Board index » cppbuilder » Itteration through Folder
Michael
CBuilder Developer |
Michael
CBuilder Developer |
Itteration through Folder2006-09-03 06:34:56 AM cppbuilder56 How do I go about iterating through a list of files in a folder. Any help would be greatly appreciated. |
Clayton Arends
CBuilder Developer |
2006-09-03 07:14:49 AM
Re:Itteration through Folder
Take a look at FindFirst(), FindNext(), and FindClose() in the help. Also,
try using the newsgroup search feature of Google called Google Groups (groups.google.com). Questions like this have been answered many times by the community. You can save yourself hours by searching there first. Hope this helps, - Clayton |
Ed Mulroy
CBuilder Developer |
2006-09-03 09:37:35 AM
Re:Itteration through Folder
See if this helps:
--------------------------- #include <windows.h> #include <vector> #include <string> class FilesInFolder { public: FilesInFolder(const char *folder_name) { std::string s = folder_name; HelpSearch(s); } FilesInFolder(const std::string & folder_name) { HelpSearch(folder_name); } ~FilesInFolder() {} unsigned NumFound() { return num_found; } const WIN32_FIND_DATA & FileInfo(unsigned index) { return (index < num_found) ? found_list[index] : null_data; } const char * FileName(unsigned index) { return (index < num_found) ? found_list[index].cFileName : ""; } bool IsDirectory(unsigned index) { return (index < num_found) ? found_list[index].dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY : false; } private: HANDLE find_handle; WIN32_FIND_DATA null_data; std::vector<WIN32_FIND_DATA>found_list; std::string folder_name; unsigned num_found; void HelpSearch(const std::string & fold_name); FilesInFolder(); FilesInFolder(const FilesInFolder &); FilesInFolder operator = (const FilesInFolder &); }; using namespace std; void FilesInFolder::HelpSearch(const std::string &fold_name) { WIN32_FIND_DATA wfd; string file_name; string srch_str = fold_name; unsigned len = srch_str.length(); if (len == 0) { srch_str = ".\\"; } else if (srch_str[srch_str.length() - 1] != '\\') { srch_str += '\\'; } srch_str += "*.*"; find_handle = FindFirstFile(srch_str.c_str(), &wfd); if (find_handle != INVALID_HANDLE_VALUE) { do { file_name = wfd.cFileName; if ((file_name == ".") || (file_name == "..")) { continue; } found_list.push_back(wfd); } while (FindNextFile(find_handle, &wfd)); FindClose(find_handle); } num_found = found_list.size(); ZeroMemory(&null_data, sizeof(null_data)); } --------------------------- . Ed QuoteMichael wrote in message {smallsort} |
maeder
CBuilder Developer |
2006-09-03 03:31:57 PM
Re:Itteration through Folder
"Michael" < XXXX@XXXXX.COM >writes:
QuoteHow do I go about iterating through a list of files in a folder. Any |