|
|
@ -1,15 +1,18 @@ |
|
|
|
#!/usr/bin/perl |
|
|
|
|
|
|
|
my $help = <<'END_HELP'; |
|
|
|
Sampling tool File Renamer |
|
|
|
usage: renamer.pl DIR SEARCHREGEX REPLACEREGEX DOIT |
|
|
|
File Renamer |
|
|
|
usage: renamer.pl DIR SEARCHREGEX REPLACEREGEX [--doit] |
|
|
|
|
|
|
|
for every regular file in DIR, sorted by mtime, |
|
|
|
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: |
|
|
|
%NR% - the index number for the file with the current sorting |
|
|
|
%MTIME% - the modification time of the file |
|
|
|
|
|
|
|
no captures support in regex :( |
|
|
|
END_HELP |
|
|
@ -18,87 +21,96 @@ END_HELP |
|
|
|
use strict; |
|
|
|
use warnings; |
|
|
|
|
|
|
|
use Data::Dumper qw(Dumper); |
|
|
|
use Data::Dumper; |
|
|
|
use POSIX qw(strftime); |
|
|
|
use Cwd; |
|
|
|
use File::stat; |
|
|
|
|
|
|
|
my $dir = "-?"; |
|
|
|
$dir = $ARGV[0]; |
|
|
|
# print(Dumper(@ARGV) . "\n"); |
|
|
|
# print("size:" . int(@ARGV) . "\n"); |
|
|
|
|
|
|
|
if (int(@ARGV) < 3) { |
|
|
|
usage(); |
|
|
|
} |
|
|
|
|
|
|
|
my $dir = $ARGV[0]; |
|
|
|
my $regexSearch = $ARGV[1]; |
|
|
|
my $regexReplace = $ARGV[2]; |
|
|
|
my $reallyDoIt = $ARGV[3]; |
|
|
|
my $reallyDoIt = $ARGV[3] || 0; |
|
|
|
|
|
|
|
if ($dir eq "-?") { |
|
|
|
usage(); |
|
|
|
if (!-d $dir) { |
|
|
|
print("$dir - no such dir" . "\n"); |
|
|
|
exit(-1); |
|
|
|
} |
|
|
|
|
|
|
|
if(!$dir) { |
|
|
|
print "Error: Need a directory.\n"; |
|
|
|
die "\n"; |
|
|
|
} |
|
|
|
if(!$regexSearch) { |
|
|
|
print "Error: Need a search regex.\n"; |
|
|
|
die "\n"; |
|
|
|
} |
|
|
|
if(!$regexReplace) { |
|
|
|
print "Error: Need a replace regex.\n\n"; |
|
|
|
die "\n"; |
|
|
|
if ($reallyDoIt eq "--doit") { |
|
|
|
$reallyDoIt = 1; |
|
|
|
} else { |
|
|
|
print("Dry run. Not renaming anything." . "\n"); |
|
|
|
} |
|
|
|
|
|
|
|
chdir($dir) || die "FATAL: cant chdir"; |
|
|
|
|
|
|
|
print getcwd()."\n"; |
|
|
|
|
|
|
|
opendir( DIR, getcwd() ); |
|
|
|
opendir(DIR, getcwd()); |
|
|
|
my @files = readdir(DIR); |
|
|
|
closedir(DIR); |
|
|
|
|
|
|
|
#filter out dirs |
|
|
|
my @filesNoDir; |
|
|
|
foreach (@files) { |
|
|
|
if(! -d $_) { |
|
|
|
push(@filesNoDir,$_); |
|
|
|
} |
|
|
|
if (!-d $_) { |
|
|
|
push(@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 $fstat = ""; |
|
|
|
my $mtime = ""; |
|
|
|
my $mtime_display = ""; |
|
|
|
my $mtime_filename = ""; |
|
|
|
|
|
|
|
my $fname_new = ""; |
|
|
|
my $counter = 1; |
|
|
|
my $regexReplaceNew = ""; |
|
|
|
my $counterFmt; |
|
|
|
for (@sorted_files) { |
|
|
|
$fname = $_; |
|
|
|
$fstat = stat($_); |
|
|
|
$mtime = strftime('%Y/%m/%d %H:%M:%S',localtime($fstat->mtime)); |
|
|
|
|
|
|
|
$regexReplaceNew = $regexReplace; |
|
|
|
$counterFmt = sprintf("%03s",$counter); |
|
|
|
$regexReplaceNew =~ s/%NR%/$counterFmt/g; |
|
|
|
|
|
|
|
$fname_new = $fname; |
|
|
|
$fname_new =~ s/$regexSearch/$regexReplaceNew/; |
|
|
|
# $fname_new =~ s/(.*?)\s1-/%NR%_$1/; |
|
|
|
|
|
|
|
printf "%-40s%-40s%-20s\n",$fname_new,$fname,$mtime; |
|
|
|
|
|
|
|
if($reallyDoIt) { |
|
|
|
rename($fname,$fname_new); |
|
|
|
} |
|
|
|
|
|
|
|
$counter++; |
|
|
|
$fname = $_; |
|
|
|
$fstat = stat($_); |
|
|
|
$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; |
|
|
|
$counterFmt = sprintf("%03s", $counter); |
|
|
|
$regexReplaceNew =~ s/%NR%/$counterFmt/g; |
|
|
|
$regexReplaceNew =~ s/%MTIME%/$mtime_filename/g; |
|
|
|
|
|
|
|
$fname_new = $fname; |
|
|
|
$fname_new =~ s/$regexSearch/$regexReplaceNew/; |
|
|
|
# $fname_new =~ s/(.*?)\s1-/%NR%_$1/; |
|
|
|
|
|
|
|
my $fname_result = $fname_new; |
|
|
|
|
|
|
|
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++; |
|
|
|
} |
|
|
|
|
|
|
|
printf "%-60s%-60s%-20s\n", $fname_result, $fname, $mtime_display; |
|
|
|
if ($reallyDoIt) { |
|
|
|
rename($fname, $fname_result); |
|
|
|
} |
|
|
|
|
|
|
|
$counter++; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sub usage { |
|
|
|
print $help; |
|
|
|
die "\n"; |
|
|
|
print $help; |
|
|
|
die "\n"; |
|
|
|
} |
|
|
|