Makefile: add pickrand
[cmccabe-bin] / bulk_rename.pl
1 #!/usr/bin/perl
2
3 #
4 # I wrote this to help me rename a group of mp3 files based on a list of
5 # tracks from a website.
6 #
7 # So, this script examines a newline-separated list of 
8 # track names and renames the files in the current directory based on that.
9 #
10 # This could be improved in some ways. The parts specific to audio files
11 # should probably be optional. But it works for me, so I'll leave it alone for
12 # now.
13 #
14 # Colin McCabe
15 # 2005/11/12 
16 #
17
18 use strict;
19
20 my $mp3_to_flac=0;
21 if ($ARGV[0] eq "-f") {
22     $mp3_to_flac=1
23 }
24
25 #Make sure there are no non-mp3 files in the directory
26 my $non_mp3_files = 
27     `find -maxdepth 1 -noleaf | grep -v mp3\$ | grep -v ^.\$ `;
28 if ($non_mp3_files) {
29     my $non_flac_files = 
30         `find -maxdepth 1 -noleaf | grep -v flac\$ | grep -v ^.\$ `;
31     if ($non_flac_files) {
32         my $non_wav_files = 
33             `find -maxdepth 1 -noleaf | grep -v wav\$ | grep -v ^.\$ `;
34         if ($non_wav_files) {
35             die "Non mp3 or flac or wav files or subdirectories found in this directory!";
36         }
37     }
38 }
39
40 #Get input from the user
41 my @cooked_lines;
42 my @input_lines = <STDIN>;
43
44 foreach my $line (@input_lines) {
45     chomp($line);
46
47     if ($line =~ '/') {
48         die "can't use slashes in filenames";
49     }
50
51     $line =~ s/[\t ]*$//; # strip trailing whitespace
52
53     if ($mp3_to_flac == 1) {
54         $line =~ s/\.mp3$/\.flac/;
55     }
56
57     if ($line =~ '\w') {
58         #Make sure this track title contains a number.
59         die "The name of this file does not contain a number." 
60             unless ($line =~ '[0-9]');
61         #If the line has some non-whitespace component, add it to the list of
62         #input lines
63         $line = TrimWhitespace($line);
64         push (@cooked_lines, $line);
65         #print "processing line..." . "\"" . $line . "\"\n"
66     }
67 }
68
69 #Make sure we have the right number of mp3s in this folder
70 my $files_in_folder = `find | grep -v ^.\$ | wc -l`;
71 chomp $files_in_folder;
72 my $number_of_input_lines = scalar(@input_lines);
73 if ($files_in_folder != $number_of_input_lines) {
74     die "$files_in_folder mp3s in the folder, but $number_of_input_lines input lines given!";
75 }
76
77
78 open(FIND_OUTPUT, "find . -maxdepth 1 | grep -v ^.\$ | sort -n | ");
79 my $find_line;
80 my $i = 0;
81 while (defined($find_line = <FIND_OUTPUT>)) {
82     chomp $find_line;
83     my $final_name = $input_lines[$i];
84     print "renaming \"$find_line\" to \"$final_name\"\n";
85     if (system("mv \"$find_line\" \"$final_name\"")) {
86         print "error!\n";
87     }
88     $i = $i + 1;
89 }
90 print "** rename complete **\n";
91 exit 0;
92
93 #Replace multiple whitespace characters with a single one
94 sub TrimWhitespace
95 {
96     my $file_name = @_[0];
97     my $old_file_name;
98     do
99     {
100         $old_file_name = $file_name;
101         $file_name =~ s#\s\s# #g;
102     } while ($file_name ne $old_file_name);
103     return "$file_name";
104 }