A common question when dealing with deep directory structures concerns how a function can be applied recursively to all the files in the target directory, regardless of the depth. This function, directory_walk, builds an internal stack (uses no recursion) and iteratively applies the user supplied callback providing a fast and flexible approach.
So, for some examples. We’ll start simple and simply print the directory:
Which for me outputs:
What if we wanted to add a .txt extension to all of these files? We could write:
Another example might be deleting all the .svn or CVS folders in a directory. We could write: