/usr/bin/editfilenames is in crip 3.9-1.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 | #!/usr/bin/perl
#
# Written to suppliment crip with a filename editor.
#
# Put preferred editor here
$editor = "sensible-editor";
# Temporary filename
use File::Temp;
$tempdir = File::Temp::tempdir(CLEANUP => 1);;
$tmpfile = "$tempdir/filelist.txt";
# Substitute spaces with an underscore (on/off - default="on")
$subsp = "on";
if ($#ARGV >= 0) {
# Chop off arguments first
$i = 0;
while ($ARGV[$i] =~ m/^-(.)/) {
$arg[$i] = $1;
$i++;
}
$numargs = $i;
# Now chop off any specific filenames passed
$j = 0;
while ($ARGV[$i] ne "") {
$list[$j] = $ARGV[$i];
$chopnl = 0;
$i++; $j++;
}
if ($j == 0) {
@list = `ls -1`;
$chopnl = 1;
}
} else {
@list = `ls -1`;
$chopnl = 1; # will need to chop off newlines from @list
}
# Now process any arguments
if ($numargs > 0) {
foreach $arg (@arg) {
if ($arg eq "p") {
print "Input the prefix to put on each file: ";
$prefix = <stdin>;
chop($prefix);
}
if (($arg eq "h") || ($arg eq "?") || ($arg eq "-")) {
print "Usage: $0 [-p] [-h] [filenames]\n\n";
print " Allows you to quickly and conveniently edit the names of a list of files\n";
print " using your favorite editor. Distributed with crip.\n\n";
print "Options:\n";
print " -p Prompt for a prefix to put onto all the filenames\n";
print " -h, -?, --help Print this help then exit\n";
print "\n";
exit(0);
}
}
}
# Create text file to edit
open(FILELIST, ">$tmpfile") || die "ERROR: Cannot open tmp file $tmpfile for writing.\n";
$index=0;
foreach $file (@list) {
$index++; $indexstr = "";
$numzeros = 4 - length($index);
foreach $i (1 .. $numzeros) { $indexstr = $indexstr . "0"; }
$indexstr = $indexstr . $index;
# Now start processing filename
if ($chopnl) { chop ($file); } # chop off the \n from the filename
# Save this as the old filename
$oldfile[$indexstr] = $file;
# Now substitute spaces
if ($subsp eq "on") {
$file =~ s/ /_/g;
}
# If there's a prefix, add it now...
if (defined $prefix) {
$file = $prefix . $file;
}
print FILELIST "$indexstr $file\n";
}
close(FILELIST);
if ($index == 0) {
print "No files.\n";
exit();
}
# Edit
system "$editor $tmpfile";
$cmdnum = 0;
open(FILELIST, "$tmpfile");
while ($line = <FILELIST>) {
chop $line;
$line =~ s/^(\d\d\d\d) //;
$index = $1;
$f1 = $oldfile[$index];
$f2 = $line;
# Escape character substitutions for $f1
$f1 =~ s/\(/\\(/g; $f1 =~ s/\)/\\)/g;
$f1 =~ s/'/\\'/g; $f1 =~ s/`/\\`/g;
$f1 =~ s/\"/\\\"/g; $f1 =~ s/ /\\ /g;
$f1 =~ s/\;/\\\;/g; $f1 =~ s/,/\\,/g;
$f1 =~ s/&/\\&/g; $f1 =~ s/\$/\\\$/g;
# Escape character substitutions for $f2
$f2 =~ s/\(/\\(/g; $f2 =~ s/\)/\\)/g;
$f2 =~ s/'/\\'/g; $f2 =~ s/`/\\`/g;
$f2 =~ s/\"/\\\"/g; $f2 =~ s/ /\\ /g;
$f2 =~ s/\;/\\\;/g; $f2 =~ s/,/\\,/g;
$f2 =~ s/&/\\&/g; $f2 =~ s/\$/\\\$/g;
unless ($f1 eq $f2) {
$cmdnum++;
$cmd[$cmdnum] = "mv -- $f1 $f2";
print "$cmd[$cmdnum]\n";
}
}
close(FILELIST);
if ($cmdnum == 0) {
print "No moves to execute.\n";
exit();
} elsif ($cmdnum == 1) {
print "Execute the above move? (y/n): ";
} else {
print "Execute the above moves? (y/n): ";
}
$inp = <stdin>; chop $inp;
if (($inp eq "y") || ($inp eq "Y")) {
print "Moving files...\n";
foreach $i (1 .. $cmdnum) {
# print "$cmd[$i]\n";
`$cmd[$i]`;
}
print "Done.\n";
} else {
print "Did not do anything.\n";
}
|