Skip to content

Instantly share code, notes, and snippets.

@note103 note103/batch_rename2.pl Secret
Last active May 18, 2016

Embed
What would you like to do?
#!/usr/bin/env perl
use strict;
use warnings;
use feature 'say';
use File::Copy 'copy';
use File::Copy::Recursive 'rcopy';
use File::Slurp 'read_file';
my $source_dir = 'source';
my $renamed_dir = 'renamed';
my $batch = 'conv.txt';
my @source_files;
my @renamed_files;
read_files();
main();
sub read_files {
@source_files = ();
@renamed_files = ();
@source_files = grep {/$source_dir\/(.+)/; $_ = $1} glob "$source_dir/*";
@source_files = sort { ($a =~ /^(\d+)/)[0] <=> ($b =~ /^(\d+)/)[0] } @source_files;
@renamed_files = grep {/$renamed_dir\/(.+)/; $_ = $1} glob "$renamed_dir/*";
}
sub main {
my $text = read_file($batch);
@renamed_files = split /\n/, $text;
unless (scalar(@source_files) == scalar(@renamed_files)) {
say "\nNot equal.\n";
say "batch:";
for (@renamed_files) {
say "\t$_";
}
say "";
say 'source files:';
for (@source_files) {
say "\t$_";
}
say '';
}
else {
say "\nfrom:";
for (@source_files) {
say "\t$_";
}
say "to:";
for (@renamed_files) {
say "\t$_";
}
say "\nMove it OK? [y/N]\n";
chomp(my $result = <STDIN>);
if ($result =~ /\A(y|yes)\z/) {
my $i = 0;
for my $source (@source_files) {
if (-f $source) {
copy("$source_dir/$source", "$renamed_dir/$renamed_files[$i]") or die $!;
}
else {
rcopy("$source_dir/$source", "$renamed_dir/$renamed_files[$i]") or die $!;
}
$i++;
}
}
else {
say "Nothing changes.\n";
}
read_files();
say 'source files:';
for (@source_files) {
say "\t$_";
}
say '';
say 'renamed files:';
for (@renamed_files) {
say "\t$_";
}
say '';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.