Browse Source

improve renamer.pl

master
heck 3 years ago
parent
commit
7d229ed191
  1. 78
      scripts/renamer.pl

78
scripts/renamer.pl

@ -1,15 +1,18 @@
#!/usr/bin/perl #!/usr/bin/perl
my $help = <<'END_HELP'; my $help = <<'END_HELP';
Sampling tool File Renamer File Renamer
usage: renamer.pl DIR SEARCHREGEX REPLACEREGEX DOIT usage: renamer.pl DIR SEARCHREGEX REPLACEREGEX [--doit]
for every regular file in DIR, sorted by mtime, for every regular file in DIR, sorted by mtime,
replaces the SEARCHREGEX with REPLACEREGEX in every filename. replaces the SEARCHREGEX with REPLACEREGEX in every filename.
Does NOT rename anything unless DOIT! if the new name clashes with an already existing file, a count
will be appended before the first dot.
Does NOT rename anything unless --doit
Special Variables are: Special Variables are:
%NR% - the index number for the file with the current sorting %NR% - the index number for the file with the current sorting
%MTIME% - the modification time of the file
no captures support in regex :( no captures support in regex :(
END_HELP END_HELP
@ -18,57 +21,56 @@ END_HELP
use strict; use strict;
use warnings; use warnings;
use Data::Dumper qw(Dumper); use Data::Dumper;
use POSIX qw(strftime); use POSIX qw(strftime);
use Cwd; use Cwd;
use File::stat; use File::stat;
my $dir = "-?"; # print(Dumper(@ARGV) . "\n");
$dir = $ARGV[0]; # print("size:" . int(@ARGV) . "\n");
my $regexSearch = $ARGV[1];
my $regexReplace = $ARGV[2];
my $reallyDoIt = $ARGV[3];
if ($dir eq "-?") { if (int(@ARGV) < 3) {
usage(); usage();
} }
if(!$dir) { my $dir = $ARGV[0];
print "Error: Need a directory.\n"; my $regexSearch = $ARGV[1];
die "\n"; my $regexReplace = $ARGV[2];
} my $reallyDoIt = $ARGV[3] || 0;
if(!$regexSearch) {
print "Error: Need a search regex.\n"; if (!-d $dir) {
die "\n"; print("$dir - no such dir" . "\n");
exit(-1);
} }
if(!$regexReplace) {
print "Error: Need a replace regex.\n\n"; if ($reallyDoIt eq "--doit") {
die "\n"; $reallyDoIt = 1;
} else {
print("Dry run. Not renaming anything." . "\n");
} }
chdir($dir) || die "FATAL: cant chdir"; chdir($dir) || die "FATAL: cant chdir";
print getcwd()."\n"; opendir(DIR, getcwd());
opendir( DIR, getcwd() );
my @files = readdir(DIR); my @files = readdir(DIR);
closedir(DIR); closedir(DIR);
#filter out dirs #filter out dirs
my @filesNoDir; my @filesNoDir;
foreach (@files) { foreach (@files) {
if(! -d $_) { if (!-d $_) {
push(@filesNoDir,$_); push(@filesNoDir, $_);
} }
} }
@files = @filesNoDir; @files = @filesNoDir;
my @sorted_files = sort { stat($a)->mtime <=> stat($b)->mtime } @files; my @sorted_files = sort {stat($a)->mtime <=> stat($b)->mtime} @files;
my $fname = ""; my $fname = "";
my $fstat = ""; my $fstat = "";
my $mtime = ""; my $mtime_display = "";
my $mtime_filename = "";
my $fname_new = ""; my $fname_new = "";
my $counter = 1; my $counter = 1;
@ -77,20 +79,30 @@ my $counterFmt;
for (@sorted_files) { for (@sorted_files) {
$fname = $_; $fname = $_;
$fstat = stat($_); $fstat = stat($_);
$mtime = strftime('%Y/%m/%d %H:%M:%S',localtime($fstat->mtime)); $mtime_display = strftime('%Y/%m/%d %H:%M:%S', localtime($fstat->mtime));
$mtime_filename = strftime('%y%m%d_%H%M%S', localtime($fstat->mtime));
$regexReplaceNew = $regexReplace; $regexReplaceNew = $regexReplace;
$counterFmt = sprintf("%03s",$counter); $counterFmt = sprintf("%03s", $counter);
$regexReplaceNew =~ s/%NR%/$counterFmt/g; $regexReplaceNew =~ s/%NR%/$counterFmt/g;
$regexReplaceNew =~ s/%MTIME%/$mtime_filename/g;
$fname_new = $fname; $fname_new = $fname;
$fname_new =~ s/$regexSearch/$regexReplaceNew/; $fname_new =~ s/$regexSearch/$regexReplaceNew/;
# $fname_new =~ s/(.*?)\s1-/%NR%_$1/; # $fname_new =~ s/(.*?)\s1-/%NR%_$1/;
my $fname_result = $fname_new;
printf "%-40s%-40s%-20s\n",$fname_new,$fname,$mtime; my $count_clash = 0;
while(-e $fname_result) {
my $count_clash_fmt = sprintf("%02s", $count_clash);
$fname_result = $fname_new =~ s/(.*?)\./$1_$count_clash_fmt./r;
$count_clash++;
}
if($reallyDoIt) { printf "%-60s%-60s%-20s\n", $fname_result, $fname, $mtime_display;
rename($fname,$fname_new); if ($reallyDoIt) {
rename($fname, $fname_result);
} }
$counter++; $counter++;

Loading…
Cancel
Save