Does anyone know of a method to recursively go through about 2000 image directories on a hard drive and 'count' how many images are located in each directory?

Also, once that has happened, is there a way to delete any images from certain directories based on the actual image count?

For example, lets say i want each directory to only have 24 images in it, if a folder has 35 images, is there a way to delete the last 11 images so that only the first 24 remain in that folder without having to go in to each of the 2000 folders and manually delete files?

Some folders have 24 images in them and some have over 100 so im hoping someone has come across this problem before and has a quick way of ensuring there are only 24 images in each of the folders, no matter how many pics are in the folder to begin with.

Each folder has images in them named from 0001.jpg thru to 0099.jpg (or whatever the highest number is).

Any help you can offer with this would be appreciated

Regards,

Lee