On Thu, Apr 01, 2004 at 07:05:51PM -0500, Morbus Iff wrote: > Earlier this morning, a friend of mine asked me for a script that would > "given a list of files, replace underscores with spaces", to take this: > > Artist_Name-Track_Name.mp3 > > and rename it to this: > > Artist Name-Track Name.mp3 > > The script was mindlessly simple, and I felt it would be a good HOWTO > for the perl beginners crowd, if not to show some good code practices, > but also to counteract the . .. .. . "controversial" HOWTO that had > been posted a week or so ago. Certainly, if you find this as misguided > as his, complain onlist with better examples, or offlist with anger. > > The first bit of code I wrote him was below. Save for > the additional explanatory comments, it's nearly exact.
[ snip ] > And that's the script. For readability, no comments: > > #!/usr/bin/perl > use warnings; > use strict; > > opendir(DIR, ".") or die $!; > my @files = readdir(DIR); > close(DIR); > > while (@files) { Are you sure that's not: for (@files) { ? > next if -d; > next if /^\./; > next unless /_/; > > my $new_name = $_; > $new_name =~ s/_/ /g; > rename($_, $new_name) or die $!; > } > > It worked fine for him, and we moved on. A few hours later, he > asked for a recursive version, and whether that would be "hard > to do". While I wasn't around to help him out, the weak solution > was easy: just move the script into each new directory and run > it again. But, there are two other solutions to this new request: > the bad one, and the good one. Here's a third: $ rename 'y/_/ /' **/*(.) That's zsh globbing and rename that used to come with perl. rename is now part of debian, and google tells me it can be found at: http://www.hurontel.on.ca/~barryp/menu-mysql/music_rename-1.12c/rename Though I'll admit that that solution doesn't provide so many opportunities for learning Perl. -- Paul Johnson - [EMAIL PROTECTED] http://www.pjcj.net -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>