I don't see anything obviously wrong, but perhaps the
rename or opendir are failing. You aren't checking, just
assuming it works. Note also that you are modifying the
directory while looping over it - this might give unexpected
results on your OS. Better to read all entries of the directory
into an array right after opening the directory, and to loop
over that.
And always check the return values of your system calls.
Abigail