How to check file exists and rename in perl

Posted by Disco on Stack Overflow See other posts from Stack Overflow or by Disco
Published on 2010-03-31T14:02:52Z Indexed on 2010/03/31 14:13 UTC
Read the original article Hit count: 288

Filed under:

I'm kinda newbie to perl and looking for a script that will handle file moving.

#!/usr/bin/perl -w
$filename = 'DUMBFILE';
$destination = '/some/location/';
if (-e $destination + $filename) {
  print "File Exists ! Renaming ..";
  move ('/tmp/' + $filename, $destination + $filename + '.1');
} else {
  move ('/tmp/' + $filename, $destination + $filename);
}

I'm able to rename it to 1, but i want to be renamed incrementally, like if file.1 exists, rename to .2, and .3 if .2 exists.

That should be easy to do, but i'm kinda lost ..

© Stack Overflow or respective owner

Related posts about perl