How to bulk rename in OS X with my requirements

Is there a utility that can rename filters in multiple subfolders and then rename the files inside sequentially?

E.g. I select my-pictures and it'll do its magic and sequentially rename all the files in all the subfolders. For each new subfolder, the numbering resets to 1.

old structure

my-pictures (parent folder)
-- folder1
---- in-costa-rica.png
---- swimming-at-beach-costa-rica.png
-- panama-folder
---- panama-canal.png
---- with-witch-doctor.png

desired/new structure

my-pictures (parent folder)
-- folder1
---- 0001.png
---- 0002.png
-- panama-folder
---- 0001.png
---- 0002.png

The following lines in Terminal should do the trick

for dir in my-pictures/*; do
    if [[ -d "$dir" ]]; then
        cd "$dir"
        i=0
        for file in *; do
            echo mv "$file" $(printf "%04d" $i).png
            (( i=i+1 ))
        done
        cd -
    fi
done

I've put in an echo statement to suppress execution so you can test it first. If the output looks ok, just remove the echo and rerun.

This assumes that there are no more than 1000 files in each subfolder, none of the files is already renamed and that all of them are PNG pictures.

By default, files are sorted by name. If you want to sort by date, replace the second for statement with

for file in $(ls -t); do