In batch or KiX you'd want to use a FOR EACH or FOR NEXT type of coding.

Don't have time to write the code for you right now, but if you search the board I'm sure you'll come up with something.