In the time that Barry and I have spent on this problem already, either of us could have written that script.
Besides his variable names suck.
What you need to do is get a list of old files (in one query), then loop over the set of files. For each file decide the new name, move the file, then update the database. You'll need to know how to figure out the new name (business knowledge, you probably have it), how to move a file (Perl's system command may help you here), and how to issue an update (my $sth = $dbh->prepare("update FOO set x = ? where y = ?") or die "Cannot prepare: $!"; then later $sth->execute("new filename", $file_id);)
If the application was sanely built there is some locking needed before you move the file out from under the application. Odds are that you aren't so lucky, so I'd suggest running your script when the application is not in use.
Cheers,
Ben