Debian Codegraph @

editfilenames

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