in reply to Checking for existing filename before renaming file

Well, you don't say what the problem is, but fortunately, I have the ultra-powerful ESP::MindRead module at hand. You're checking whether $oldfile exists, but what you really want is to check that the target file doesn't exist. That is, the one contained in $_ at this point:

my $oldfile = $_; s/ /_/g;
In other words, test if (! -e $_)

If that's not the problem, could you please state what it is? ;-)