开发者

How to check file exists and rename in perl

Possible duplicate: Renaming and Moving Files in Bash or Perl

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.

EDIT : and keep the extension the same; like file.exe becomes file.exe.1, f开发者_Python百科ile.exe.2 etc.


You need to concatenation . instead of +:

if (-e $destination . $filename) {

Or even better. You can use File::Spec module:

use File::Spec;
...
if (-e File::Spec->join($destination, $filename)) {

And please use strict;

To move file you can use File::Copy module:

use File::Copy;
...
move($from, $to);


Probably what you want to do is something like:

use File::Copy;

sub construct_filename {
    my ($dir, $name, $counter) = @_;
    if ($name =~ /^(.*)\.(.*)$/) {
        return "$dir/$1.$counter.$2";
    } else {
        return "$dir/$name.$counter";
    }
}

if (-e "$destination$filename") {
    $counter = 1;
    while (-e construct_filename($destination,$filename,$counter)) {
        $counter++;
    }
    move("/tmp/$filename", construct_filename($destination,$filename,$counter));
} else {
    move("/tmp/$filename", "$destination$filename");
}

Also, the string concatenation operator in perl is ., not +. What you wrote isn't going to work at all. In this case you can just use double-quoted string interpolation though, and not bother with concatenation.

And for what it's worth, an easier convention is for directories to never contain trailing slashes, and to always use them when you construct filenames (e.g. "$destination/$filename"). As others have pointed out, the most robust way to do the path-building is with File::Spec. From what I could tell, though, the part you were really looking for was how to do the incremental naming; so I've given a clear and short version of that. Upgrade to File::Spec as you see fit!


#!/usr/bin/perl
use strict;
use warnings;

use File::Spec::Functions qw'catfile';
use File::Copy qw'move';
use autodie    qw'move';

my $filename    = 'DUMBFILE';
my $origin      = '/tmp';
my $destination = '/some/location';

my $path = catfile $destination, $filename;
{
  my $counter;
  while( -e $path ){
    $counter++;
    $path = catfile $destination, "$filename.$counter";
  }
}

move( catfile( $origin, $filename ), $path );


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜