#!/usr/bin/perl # # Written to suppliment crip with a filename editor. # # Put preferred editor here $editor = "vim"; # Temporary filename $pid = $$; # Unique process ID of this process $tmpfile = "/tmp/filelist_" . $pid . ".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 = ; 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 = ) { 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 = ; 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"; }