59 lines
1.4 KiB
Perl
Executable File
59 lines
1.4 KiB
Perl
Executable File
#!/usr/bin/env perl
|
|
|
|
use strict;
|
|
use warnings;
|
|
|
|
my ($fromdir, $renamefile, $todir) = @ARGV;
|
|
|
|
sub usage {
|
|
die "usage: $0 [fromdir] [renamefile] [todir]\n
|
|
takes files from [fromdir], renames according to the json-map in
|
|
[renamefile] and writes the files to [todir].
|
|
old filenames are used as keys; new filenames as value\n";
|
|
}
|
|
|
|
usage() unless $fromdir and $renamefile and $todir;
|
|
usage() unless -d $fromdir;
|
|
usage() unless -f $renamefile;
|
|
|
|
mkdir $todir;
|
|
|
|
my %did = ();
|
|
|
|
my @errNex = ();
|
|
|
|
my $fh = undef;
|
|
open($fh, '<', $renamefile) or die "Cannot read '$renamefile', because: $!";
|
|
my $cont = join '', <$fh>;
|
|
close $fh;
|
|
if($cont!~m#^\s*\{(.*)\}\s*$#s) { die "'$renamefile' not in an expected format; it should be an json-object" }
|
|
my $core = $1;
|
|
|
|
while($core=~s#^\s*,?\s*"([^"/]+)"\s*:\s*"([^"/]+)"##) {
|
|
my ($from, $to) = ($1, $2);
|
|
my $pfrom = "$fromdir/$from";
|
|
my $pto = "$todir/$to";
|
|
if(-e $pfrom) {
|
|
print "Renaming '$pfrom' to '$pto'\n";
|
|
system("cp", $pfrom, $pto);
|
|
$did{$from} = 1;
|
|
} else {
|
|
push @errNex, $from
|
|
}
|
|
}
|
|
|
|
for(@errNex) {
|
|
warn "Could not rename non-existent file: $_\n";
|
|
}
|
|
|
|
die "Syntax error in [renamefile], could not process everything!" if $core!~m#^[\s*,]*$#;
|
|
|
|
my $dh = undef;
|
|
opendir($dh, $fromdir) or die "Could not read dir '$fromdir', because: $!";
|
|
while(my $filename = readdir($dh)) {
|
|
next if $filename=~m#^\.\.?$#;
|
|
warn "Did not touch not mentioned file '$filename'\n" unless $did{$filename};
|
|
}
|
|
|
|
|