Debian Codegraph @

editfilenames

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