Essential Tools Module User's Guide : Chapter 10 File System Classes : RWDirectoryIterator
RWDirectoryIterator
RWDirectoryIterator is a template class which represents a portable iterator over the content of a file system directory. RWDirectoryIterator has one specialization (const char*) suited for flat iteration (i.e., no recursive iteration of directories) and one (RWCString) which is recursive.
Both specializations are: assignable, copy constructible, equality comparable.
 
RWDirectoryIterator<const char*> iter ("somedir"); // OK
RWDirectoryIterator<const char*> iter1 (iter); // OK
iter1 = iter; // OK
bool b = iter == iter1; // OK
Flat Iteration Example
void ls (const char* s)
{
// Path
RWCString path (s);
std::cout << "Listing content of : " << s << std::endl;
// Create the iterator
RWDirectoryIterator<const char*> it (s);
// Iterate until the iterator compares equal with the end
// iterator (RWDirectoryIterator<const char*> ())
for(; it != RWDirectoryIterator<const char*> (); ++it) {
std::cout << std::setw (40) << std::left << *it;
RWCString filename (path);
filename += PATH_SEPARATOR;
filename += *it;
// Print the filename
std::cout << filename << "\n";
}
std::cout << std::endl;
}
Recursive Iteration Example
void ls_R (const char* s, int depth = 0)
{
std::cout << "Recursively listing content of : " << s
<< " (depth: " << depth << ")\n";
// Create the iterator
RWDirectoryIterator<RWCString> it (s, depth);
// Iterate until iterator compares equal with end iterator
// (RWDirectoryIterator<RWCString> ())
for (; it != RWDirectoryIterator<RWCString> (); ++it) {
std::cout << std::setw (60) << std::left << *it;
// Print the filename
std::cout << *it << "\n";
}
std::cout << std::endl;
}