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:
found target-dir/foo
found target-dir/bar
found target-dir/baz
found target-dir/baz/ding
found target-dir/baz/dong
found target-dir/baz/dong/witch
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: