in reply to Cleaning up an iTunes music collection

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.

Replies are listed 'Best First'.
Re: •Re: Cleaning up an iTunes music collection
by mvaline (Friar) on Apr 30, 2003 at 21:22 UTC
    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; } }