-
Notifications
You must be signed in to change notification settings - Fork 2
/
file_renamer.pl
executable file
·93 lines (83 loc) · 2.92 KB
/
file_renamer.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
#!/usr/bin/perl -w
# program that strips words from a filename
# it also now removes bad things from the filename
# usage: filename_strip.pl /path/to/mp3s word_to_strip
#
# (c)2000 Brian Manning
# external modules
use Getopt::Std;
use strict;
# variables
my $DEBUG; # are we debugging?
my %opts; # command line options hash
my @filelist; # list of files, built with glob or a real list of files
my ($file, $newname); # a file from @filelist, new file name
my @splitname; # full path split up
### begin script ###
# get the command line switches
&getopts("dhlCve:f:p:w:", \%opts);
if ( exists $opts{h} ) {
warn "Usage: file_renamer.pl [options]\n";
warn "[options] consist of:\n";
warn " -h show this help list\n";
warn " -d Debug mode - Doesn't do anything destructive. " .
"Automagically sets -v\n";
warn " -v Verbose mode - extra noisy\n";
warn " -w word to replace in the filename " .
" (cannot be combined with -f)\n";
warn " -C be case sensitve in matching words to replace\n";
warn " -f filename containing a list of files to parse/rename\n";
warn " -e filename extension to match when not using a filelist\n";
warn " -p path to files when not using a filelist\n";
warn " -l just lowercase the filenames found with -e and -p\n";
# exit out
exit 0;
} # if ( exists $opts{h} )
# was a filename extension passed in?
if ( exists $opts{e} && exists $opts{p} ) {
@filelist = <$opts{p}/*.$opts{e}>;
} elsif ( exists $opts{f} ) {
open (LIST, $opts{f});
@filelist = <LIST>;
close (LIST);
} else {
die "Please pass either a -f (list of files) or a -p (path) and " .
"-e (extension)\nto filter a specific set of files";
} # if ( exists $opts{e} && exists $opts{p} )
foreach $file (@filelist) {
chomp($file);
if ( $opts{d} || $opts{v} ) { warn "Stripping $file\n";}
# split the full filename, this is so we don't do strange shit to the
# path
@splitname = split("/", $file);
chomp(@splitname);
# copy the old name over to $newname
$newname = $splitname[-1];
if ( $opts{d} || $opts{v} ) { warn "original filename is $newname\n";}
# is there a file extension?
if ( exists $opts{e} ) {
if ( ! exists $opts{w} ) { $opts{w} = "";}
# are we case-sensitive matching on a pattern
if ( $opts{C} ) {
$newname =~ s/$opts{w}//;
# no, we're case-insensitve matching on a pattern
} else {
$newname =~ s/$opts{w}//i;
} # if ( $opts{C} )
} # if ( exists $opts{e} )
$newname =~ s/&/-n-/g;
$newname =~ s/ /_/g;
# are we just lowercasing the filename?
if ( $opts{l} ) { $newname = lc($newname);}
$splitname[-1] = $newname;
if ( $opts{d} ) {
warn "would have renamed $file\n to new name " .
join("/",@splitname) . "\n\n";
} else {
if ( $opts{v} ) {
warn "renaming $file\nto " . join("/",@splitname);
} # if ( $opts{v} )
rename ( "$file", join("/",@splitname) ) ||
die "Couldn't rename $file : $!";
} # if ( $opts{d} )
} # foreach