-
Notifications
You must be signed in to change notification settings - Fork 1
/
rename.pl
executable file
·498 lines (395 loc) · 14.4 KB
/
rename.pl
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
#!/usr/bin/perl -w
#rename.pl
#Robert W. Leach
#10/28/2004
#Los Alamos National Laboratory
#Copyright 2004
markTime();
my $software_version_number = '1.10';
##
## Start Main
##
use strict;
use Getopt::Long;
#Declare variables
my($quiet,
$help,
@input_files,
$version,
$verbose,
$force);
my $append_flag = 0;
my $replace = 'rename_placeholder';
my $with = 'rename_placeholder';
my $pattern_mode = 0;
my $number_mode = 0;
my $increment = 0;
my $dry_run = 0;
my $DEBUG = 0;
#If there are no arguments and no files directed in
if(scalar(@ARGV) == 0)
{
usage();
exit(0);
}
#Get the input options
GetOptions('r|replace=s' => \$replace, #REQUIRED if -w not supplied
'w|with=s' => \$with, #REQUIRED if -r not supplied
'p|pattern-mode!' => \$pattern_mode, #OPTIONAL [Off]
'n|number-mode!' => \$number_mode, #OPTIONAL [Off]
'skip-existing-nums!'=> \$increment, #OPTIONAL [Off]
'd|dry-run!' => \$dry_run, #OPTIONAL [Off]
'f|force!' => \$force, #OPTIONAL [Off]
'version!' => \$version, #OPTIONAL [Off]
'q|quiet!' => \$quiet, #OPTIONAL [Off]
'v|verbose!' => \$verbose, #OPTIONAL [Off]
'h|help!' => \$help, #OPTIONAL [Off]
'<>' => sub {push(@input_files,$_[0])}
#OPTIONAL [All files]
);
#If the user has asked for help, call the help subroutine
if($help)
{
help();
exit(0);
}
#If the user has asked for the software version, print it
if($version)
{
print($software_version_number,"\n");
exit(0);
}
#If either of the replace or with flags is not supplied, assume the 1st and 2nd
#non-files are the replace and with strings and turn off the force flag
my @not_files = grep {/\S/ && !(-e $_)} map {glob($_)} @input_files;
if(scalar(@not_files) && $replace eq 'rename_placeholder')
{
$replace = shift(@not_files);
@input_files = grep {$_ ne $replace} @input_files;
if($force)
{warning("The force flag will not work without an explicit 'replace' ",
"flag.")}
$force = 0;
}
if(scalar(@not_files) && $with eq 'rename_placeholder' && !$number_mode)
{
$with = shift(@not_files);
@input_files = grep {$_ ne $with} @input_files;
if($force)
{warning("The force flag will not work without either an explicit ",
"-w(--with) flag or -n(--number-mode) flag.")}
$force = 0;
}
if($with ne 'rename_placeholder' && $number_mode)
{
error("You cannot supply -w and -n together.");
usage() unless($quiet);
exit(1);
}
if($replace eq 'rename_placeholder' && $with eq 'rename_placeholder' &&
!$number_mode)
{
error("You must supply either the -r, -w, or -n parameter.")
unless($quiet);
usage() unless($quiet);
exit(2);
}
if($replace eq 'rename_placeholder')
{
$replace = '';
$append_flag = 1;
}
$with = '' if($with eq 'rename_placeholder');
if(scalar(@input_files) == 0)
{
my $globstring = $replace;
$globstring =~ s/(.*)(\/|\A)/$1$2*/;
if(defined($replace) && !$pattern_mode)
{@input_files = grep {-e $_} glob("$globstring*")}
else
{@input_files = glob('*')}
}
#If they specified a file path in the pattern, remove it
$replace =~ s/.*\///;
$replace = quotemeta($replace) if(defined($replace) && !$pattern_mode);
debug("Pattern: /$replace/$with/");
my $renamed = 0;
my $number = 1;
my $exist_check = {};
#For each input file of the same type
foreach my $input_file (@input_files)
{
next unless(-e $input_file);
my($newname);
do
{
$newname = $input_file;
$with = $number if($number_mode);
if($append_flag)
{
debug("Appending");
$newname .= $with;
}
else
{
debug("$newname =~ s/(?=[^\\/]*\\Z)$replace/$with/");
$newname =~ s/(?=[^\/]*\Z)$replace/$with/;
debug("Replaced: $newname");
}
#If we're in "skip existing nums" mode and the file exists, increment
if($input_file ne $newname && $number_mode && $increment &&
-e $newname)
{$number++}
}
#If we're in "skip existing nums" mode and the file exists, loop
while($input_file ne $newname && $number_mode && $increment &&
-e $newname);
debug("$input_file -> $newname");
if($input_file eq $newname)
{next}
elsif(!$force && -e $newname)
{
$number++; #Advance the number anyway so they can force it later
error("$newname already exists. Use -f to override.") if(!$quiet);
}
else
{
warning("Replacing $newname") if(!$quiet && -e $newname);
if($verbose)
{print("Renaming [$input_file] to [$newname].\n")}
if(!-w $input_file || !-W $input_file)
{error("Unable to rename [$input_file]. No write permission. ")}
else
{
my $status = rename($input_file,$newname) unless($dry_run);
if(!$? && $status && !exists($exist_check->{$newname}) &&
-e $newname && !-e $input_file)
{
$exist_check->{$newname} = 1;
$renamed++;
}
elsif($? || !$status || exists($exist_check->{$newname}))
{
error("Unable to rename [$input_file]. ",
($dry_run && exists($exist_check->{$newname}) ?
"This rename operation will conflict with a " .
"previously renamed file." : $!),"\n");
}
else
{error("Unable to rename [$input_file]. Reason unknown.")}
}
$number++; #Advance the number in any case so they can force it later
}
}
if($dry_run)
{print("$renamed files will be renamed.\n") unless($quiet)}
else
{print("$renamed files successfully renamed.\n") unless($quiet)}
##
## End Main
##
##
## Subroutines
##
sub help
{
#Print a description of this program
print << "end_print";
$0
Copyright 2004
Robert W. Leach
10/28/2004
Bioscience Division
Bioinformatics and Computational Biology Group, B5
Biodefense Informatics Team
MS M888
Los Alamos National Laboratory
Los Alamos, NM 87545
robleach\@lanl.gov
* WHAT IS THIS: This script takes a string to replace and/or a string to substitute/append and changes all supplied (or matching) files in the current (or supplied) directory (or directories).
end_print
return(0);
}
sub usage
{
print << "end_print";
USAGE: $0 -r|--replace replace_string -w|--with with_string input_files [-p] [-f|--force] [-h|--help] [--version] [-q|--quiet]
OPTIONAL [All files] Input file(s).
-r|--replace REQUIRED Replace string. This is a string in a group
of file names that you want to change. Not
required if -w is supplied. If -w is not
supplied, the string is removed from the
name. You may use globbed file names, but
they cannot be interpolated by the shell, so
you must use quotes around the replace
string like this: "*/replace_string".
This will find a file in every directory
containing 'replace_string' in its name to
make the replacement.
-w|--with REQUIRED With string. This is the string you want to
add to the file names. If -r is supplied,
the with string is substituted into its
place. If -r is not supplied, the file name
is appended with the with string.
-p|--pattern-mode OPTIONAL [Off] Treat the -r string as a perl regular
expression.
-n|--number-mode OPTIONAL [Off] Supplying this flag indicates that the
replace string is to be replaced by an
incrementing number (starting from 1). The
number is incremented for each file name
successfully changed. If -r is not
supplied, the number is appended to the file
name. Cannot use with -w (--with).
--skip-existing-nums OPTIONAL [Off] Increment the number (see -n) until
the new file name is unique (i.e. avoids
overwriting existing files). Only used if
-n is supplied.
-f|--force OPTIONAL [Off] Force file rename. Use this if you
want the rename operation to overwrite
possible files that have the same name as
the new names being generated.
-q|--quiet OPTIONAL [Off] Quiet mode. Turns off warnings and
errors.
-h|--help OPTIONAL [Off] Help. Use this option to see sample
input files and an explanation.
--version OPTIONAL [Off] Print software version and quit.
end_print
return(0);
}
##
## Subroutine that prints errors with a leading program identifier
##
sub error
{
#Gather and concatenate the error message and split on hard returns
my @error_message = split("\n",join('',@_));
pop(@error_message) if($error_message[-1] !~ /\S/);
my $script = $0;
$script =~ s/^\.\///;
#Assign the values from the calling subroutine
#but if called from main, assign the values from main
my($junk1,$junk2,$line_num,$calling_sub);
(($junk1,$junk2,$line_num,$calling_sub)) = caller(1) ||
(($junk1,$junk2,$line_num) = caller());
#Edit the calling subroutine string
$calling_sub =~ s/^.*?::(.+)$/$1:/ if(defined($calling_sub));
#Put location information at the beginning of each line of the message
foreach my $line (@error_message)
{
print STDERR ("ERROR:$script:LINE$line_num:",
(defined($calling_sub) ? $calling_sub : ''),
' ')
if($line =~ /\S/);
print STDERR ($line,"\n");
}
#Return success
return(0);
}
##
## Subroutine that prints warnings with a leader string
##
sub warning
{
#Gather and concatenate the warning message and split on hard returns
my @warning_message = split("\n",join('',@_));
pop(@warning_message) if($warning_message[-1] !~ /\S/);
#Put leader string at the beginning of each line of the message
foreach my $line (@warning_message)
{print STDERR (($line =~ /\S/ ? 'WARNING: ' : ''),$line,"\n")}
#Return success
return(0);
}
##
## Subroutine that gets a line of input and accounts for carriage returns that
## many different platforms use instead of hard returns. Note, it uses a
## global array reference variable ($infile_line_buffer).
##
sub getLine
{
my $file_handle = $_[0];
#Set a global array variable if not already set
$main::infile_line_buffer = {} if(!defined($main::infile_line_buffer));
if(!exists($main::infile_line_buffer->{$file_handle}))
{$main::infile_line_buffer->{$file_handle} = []}
#If this sub was called in array context
if(wantarray)
{
#Check to see if this file handle has anything remaining in its buffer
#and if so return it with the rest
if(scalar(@{$main::infile_line_buffer->{$file_handle}}) > 0)
{return(@{$main::infile_line_buffer->{$file_handle}},
map {split("\r",$_)} <$file_handle>)}
#Otherwise return everything else
return(map {split("\r",$_)} <$file_handle>);
}
#If the file handle's buffer is empty, put more on
if(scalar(@{$main::infile_line_buffer->{$file_handle}}) == 0)
{@{$main::infile_line_buffer->{$file_handle}} = split("\r",
<$file_handle>)}
#Shift off and return the first thing in the buffer for this file handle
return($_ = shift(@{$main::infile_line_buffer->{$file_handle}}));
}
sub debug
{
return unless($DEBUG);
#Gather and concatenate the error message and split on hard returns
my @debug_message = split("\n",join('',@_));
pop(@debug_message) if($debug_message[-1] !~ /\S/);
#Assign the values from the calling subroutine
#but if called from main, assign the values from main
my($junk1,$junk2,$line_num,$calling_sub);
(($junk1,$junk2,$line_num,$calling_sub)) = caller(1) ||
(($junk1,$junk2,$line_num) = caller());
#Edit the calling subroutine string
$calling_sub =~ s/^.*?::(.+)$/$1:/ if(defined($calling_sub));
#Put location information at the beginning of each line of the message
foreach my $line (@debug_message)
{
print STDERR ("DEBUG:LINE$line_num:",
(defined($calling_sub) ? $calling_sub : ''),
' ')
if($line =~ /\S/);
print STDERR ($line,"\n");
}
#Return success
return(0);
}
#This sub marks the time (which it pushes onto an array) and in scalar context
#returns the time since the last mark by default or supplied mark (optional)
#In array context, the time between all marks is always returned regardless of
#a supplied mark index
#A mark is not made if a mark index is supplied
#Uses a global time_marks array reference
sub markTime
{
#Record the time
my $time = time();
#Set a global array variable if not already set
$main::time_marks = [] if(!defined($main::time_marks));
#Read in the time mark index or set the default value
my $mark_index = (defined($_[0]) ? $_[0] : -1); #Optional Default: -1
#Error check the time mark index sent in
if($mark_index > (scalar(@$main::time_marks) - 1))
{
error("Supplied time mark index is larger than the size of the ",
"time_marks array.\nThe last mark will be set.");
$mark_index = -1;
}
#Calculate the time since the time recorded at the time mark index
my $time_since_mark = (scalar(@$main::time_marks) ?
($time - $main::time_marks->[$mark_index]) : 0);
#Add the current time to the time marks array
push(@$main::time_marks,$time)
if(!defined($_[0]) || scalar(@$main::time_marks) == 0);
#If called in array context, return time between all marks
if(wantarray)
{
if(scalar(@$main::time_marks) > 1)
{return(map {$main::time_marks->[$_ - 1] - $main::time_marks->[$_]}
(1..(scalar(@$main::time_marks) - 1)))}
else
{return(())}
}
#Return the time since the time recorded at the supplied time mark index
return($time_since_mark);
}