Setting up and using a regular expression in order to filter the content of files is certainly the main task of this recipe. However, let's concentrate on recursive_directory_iterator because filtering recursively iterated files was just our motivation to use this special iterator class in this recipe.
Just like directory_iterator, recursive_directory_iterator iterates over elements of a directory. Its specialty is to do this recursively, as its name tells. Whenever it hits a filesystem element that is a directory, it will yield a directory_entry instance to this path, but then also descend down into it in order to iterate its children, too.
recursive_directory_iterator has some interesting member functions:
- depth(): This tells us how many levels the iterator has currently descended down into subdirectories.
- recursion_pending(): This tells us if the iterator is going to descend down after the element it currently points to.
- disable_recursion_pending(): This can be called to keep the iterator from descending into the next subdirectory if it is currently pointing to a directory into which it would descend. This means that calling this method has no effect if we call it too early.
- pop(): This aborts the current recursion level and goes one level up in the directory hierarchy to continue from there.