Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Cleaning up an iTunes music collection

by mvaline (Friar)
on Apr 30, 2003 at 16:05 UTC ( [id://254353]=CUFP: print w/replies, xml ) Need Help??

I use iTunes running on Mac OS X to create and manage my mp3 collection. It's a great piece of software, but it unfortunately doesn't give the user any control over the format of the filenames and directories it outputs. They consequently contain not only annoying characters like spaces, pound signs and parentheses, but also dangerous characters like ampersands, commas and apostrophes. I use this script to make my music directory a little more Internet friendly.
#!/usr/bin/perl # mp3clean: make an iTunes music collection more Internet friendly. # Micah Valine <micah@micahvaline.com> use strict; use File::Find; die "Usage: mp3clean [Music Root Directory]\n" unless @ARGV == 1; my $start_directory = $ARGV[0]; print "\nmp3clean is designed to make an iTunes music collection more +Internet friendly\n"; print "by renaming files and directories in a more appropriate manner. + Running it on a directory\n"; print "other than a directory of mp3's may cause permanent damage.\n\n +"; print "Press Enter to run mp3clean on [$start_directory]. "; my $user_response = <STDIN>; finddepth(\&cleanup, $start_directory); print "\nmp3clean complete!\n"; sub cleanup() { # get rid of these annoying files while we're at it if ($_ eq ".DS_Store") { unlink $_; } if ($_ eq ".FBCIndex") { unlink $_; } if ($_ eq ".FBCSemaphoreFile") { unlink $_; } if ($_ eq ".FBCLockFolder") { rmdir $_; } my $original_filename = $_; s/\'//g; s/\,//g; s/\#//g; s/&/and/g; s/!//g; s/\(//g; s/\)//g; s/ /_/g; $_ = lc($_); rename($original_filename, $_); }

Replies are listed 'Best First'.
•Re: Cleaning up an iTunes music collection
by merlyn (Sage) on Apr 30, 2003 at 20:40 UTC
    rename($original_filename, $_);
    Ouch. That can lead to lossage if two names collide. Consider instead:
    if (-e) { warn "Cannot rename $original_filename to $_: file exists\n"; } elsif (!rename($original_filename, $_)) { warn "Cannot rename $original_filename to $_: $!\n"; }

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

      Ouch... that was foolish of me. Thanks!

      In addition, the fact that I wasn't checking for errors on the rename operation allowed me to get away with not seeing a couple serious logic errors: this script occasionally attempts to rename files it has already deleted in addition to trying to rename the "." and ".." directory entries.

      Updated code follows:

      #!/usr/bin/perl -w # mp3clean: make an iTunes music collection more Internet friendly. # Micah Valine <micah@micahvaline.com> use strict; use File::Find; die "Usage: mp3clean [Music Root Directory]\n" unless @ARGV == 1; my $start_directory = $ARGV[0]; print "\nmp3clean is designed to make an iTunes music collection\n"; print "more Internet friendly by renaming files and directories in\n"; print "a more appropriate manner. Running it on a directory other\n"; print "than a directory of mp3's may cause permanent damage.\n\n"; print "Press Enter to run mp3clean on [$start_directory]. "; my $user_response = <STDIN>; finddepth(\&cleanup, $start_directory); print "\nmp3clean complete!\n"; sub cleanup() { # get rid of these annoying files while we're at it if (($_ eq ".") || ($_ eq "..")) { return 0; } if ($_ eq ".DS_Store") { unlink $_; return 0; } elsif ($_ eq ".FBCIndex") { unlink $_; return 0; } elsif ($_ eq ".FBCSemaphoreFile") { unlink $_; return 0; } elsif ($_ eq ".FBCLockFolder") { rmdir $_; return 0; } else { my $original_filename = $_; s/\'//g; s/\,//g; s/\#//g; s/&/and/g; s/!//g; s/\(//g; s/\)//g; s/ /_/g; $_ = lc($_); if (-e) { warn "Cannot rename $original_filename to $_: file exists\ +n"; } elsif (!rename($original_filename, $_)) { warn "Cannot rename $original_filename to $_: $!\n"; } return 0; } }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: CUFP [id://254353]
Approved by Paladin
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2024-04-25 14:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found