summaryrefslogtreecommitdiff
path: root/dot/local/bin/interactive-rename
blob: e8017c7ebfd57c13257753f53be3935057d303e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#!/usr/bin/perl -w
use File::Temp ();
$tmp = new File::Temp(UNLINK=>1);

# TODO:
# escape & unescape linefeeds
# handle nonexistent files better
# handle system() errors

@ARGV = grep {-e $_} @ARGV;
die "No files!\n" unless @ARGV;

for (@ARGV) {
  m(\n) && die "Sorry - can't do filenames with linefeeds (yet)\n";
  print $tmp "$_\n";
}

$editor = $ENV{VISUAL} || $ENV{EDITOR} || 'vi';
system($editor, $tmp) == 0 or die;

seek($tmp, 0, 0) or die "seek: $!";
@newname = <$tmp>;
chomp for @newname;
die "number of names changed!\n" unless @newname == @ARGV;

for (0 .. $#ARGV) {
  rename($ARGV[$_], $newname[$_]) unless $ARGV[$_] eq $newname[$_];
}