tired of getting whitespaces in filenames (mp3, pr0n, bosses' latest decree)?
( @ARGV ) || die "Usage : $0 /some/directory /another/directory\n"; foreach $directory ( @ARGV ) { if ( -d $directory ) { chdir("$directory"); convert_dir(); chdir("-"); } } sub convert_dir { chomp( my $dir = `pwd`); opendir(DIR,"$dir"); while ( my $file = readdir DIR ) { my $oldname = "$file"; $file =~ s/\s+/_/g; # print "Renaming $oldname to $file\n"; rename("$oldname","$file"); } close(DIR); return 1; } ___ | | | x | <---bang head here if this |___| frustrates you

Replies are listed 'Best First'.
Re: convert whitespaces to underscores
by Juerd (Abbot) on May 26, 2002 at 00:03 UTC

    Just a thought:

    perl -e'($z = $_) =~ s/\s+/_/g, rename $_, $z or warn "$_: $!\n" for @ +ARGV' /some/directory/* /another/directory/*

    - Yes, I reinvent wheels.
    - Spam: Visit eurotraQ.

Re: convert whitespaces to underscores
by mattriff (Chaplain) on May 26, 2002 at 03:09 UTC
    A few thoughts:

    I'm pretty sure that chdir("-") will only work under certain UNIX shells. Actually, in limited testing, it doesn't seem to work for me even when the Perl script is run from a shell (tcsh) that does support that.

    UPDATE: In retrospect, as merlyn has pointed out, this would in fact never work. My pre-post testing actually bore that out. No more late (for me) night posting...

    You can omit the quotes around lone variable names. That is, "$file" and $file are the same in all of your cases.

    As an aside, if you use the Cwd module and its getcwd() function, your code will be portable to non-UNIX systems (that probably don't have a pwd command to shell out to). You don't really need to use either method, though. Since you are presumably in the directory you want to open, you can use: opendir(DIR,'.').

    I say presumably because the code really should be checking to see if the chdir() you did succeeded, or you could end up with unexpected results. Likewise, you might consider using the -e test to see if a file named $file already exists before you rename(), so you don't overwrite something accidentally.

    - Matt Riffle

      chdir "-"; won't work anywhere. All I can say is "untested code". It doesn't matter what your shell is. Perl is handing that to chdir(2) which is then looking (regardless of shell) for a subdirectory called dash underneath the current directory.

      That's why you should always always always check the return status of chdir -- to prevent such embarassment.

      -- Randal L. Schwartz, Perl hacker

(crazyinsomniac) Re: convert whitespaces to underscores
by crazyinsomniac (Prior) on May 26, 2002 at 22:29 UTC

      (tr/// vs s///)

      It doesn't matter for this script as it doesn't matter for most scripts. This particular script is slow because of forking and filesystem changes. Changing the s/// to a tr/// won't increase the speed notably. Not even for a million MP3 files.

      s///g may not be the best way, but it is certainly not a bad way.

      - Yes, I reinvent wheels.
      - Spam: Visit eurotraQ.

Re: convert whitespaces to underscores
by Galen (Beadle) on Jun 24, 2002 at 19:32 UTC
    Just a thought, but if you are changing all filename whitespaces to underscores, shouldn't you be doing the same for the directories?