Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- use File::Copy;
- sub readAll {
- my ($file) = @_;
- open INPUT,'<',$file;
- my @lines = <INPUT>;
- close INPUT;
- return @lines;
- }
- sub myBase {
- my ($path) = @_;
- if ($path =~ /(.*)\/(.*)/) {
- $path = $2;
- } else {
- print STDERR "myBase error: path with no slashes\n";
- exit(1);
- }
- return $path;
- }
- sub myDir {
- my ($path) = @_;
- if ($path =~ /(.*)\/(.*)/) {
- $path = $1;
- } else {
- print STDERR "myDir error: path with no slashes\n";
- exit(1);
- }
- return $path;
- }
- sub fixLocal {
- my ($path) = @_;
- if ($path =~ /(.*)\/(.*)/) {
- } else {
- $path = "./" . $path;
- }
- return $path;
- }
- sub incSuffix {
- my ($path) = @_;
- $dir = myDir $path;
- $base = myBase $path;
- my $basebase;
- my $suffix;
- my $ext;
- if ($base =~ /(.*)_([0-9]*)(\..{1,5})/) {
- $basebase = $1;
- $suffix = $2;
- $ext = $3;
- } elsif ($base =~ /(.*)_([0-9]*)/){
- $basebase = $1;
- $suffix = $2;
- $ext = "";
- } elsif ($base =~ /(.*)(\..{1,5})/) {
- $basebase = $1;
- $suffix = 0;
- $ext = $2;
- } else {
- $basebase = $base;
- $suffix = 0;
- $ext = "";
- }
- $path = $dir . "/" . $basebase . "_" . ($suffix+1) . $ext;
- return $path;
- }
- #my @samples = ('foo/bar/meh','foo/bar/meh_1','foo/bar/meh_3.pdf','foo/bar/meh.pdf');
- #for my $sample (@samples) {
- # print $sample . " dir: " . myDir($sample) . " base: " . myBase($sample) . " incSuffix: " . incSuffix($sample) . "\n";
- #}
- #exit(0);
- sub safeMove {
- my ($src,$dst) = @_;
- $src = fixLocal $src;
- $dst = fixLocal $dst;
- if ( -f $src ) {
- if ( -d $dst ) {
- $dst = $dst . "/" . myBase($src);
- }
- while ( -f $dst ) {
- $dst = incSuffix $dst;
- }
- print STDERR $src . " -> " . $dst . "\n";
- move($src,$dst) or print STDERR "move error: " . $! . "\n";
- } else {
- print STDERR "error src not exist: " . $src . "\n";
- }
- }
- my ($file) = @ARGV;
- my @lines = readAll $file;
- for my $line (@lines) {
- chomp($line);
- if ($line =~ /(.*)\t(.*)/) {
- my $src = $1;
- my $dst = $2;
- safeMove $src,$dst;
- } else {
- print STDERR "wrong format: " . $line . "\n";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement