I haven't done it exactly like that but it's easy.
I would first rename the original files so they don't confuse the code to oldname*.jpg to avoid an infinite loop or something. Then write a simple loop that reads the directory, counts each file, and renames them or, instead of renaming them first, just load the list into an array.
Off the top of my head something like this would work, I didn't run it, didn't test it, just showing how it could be done.
If the files need to maintain the same order you would load them into an array first and sort it, then iterate the file names from the array instead of current(), which also avoids renaming them in advance as mentioned above.