Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

convert whitespaces to underscores

by painehope (Initiate)
on May 25, 2002 at 23:41 UTC ( #169321=CUFP: print w/replies, xml ) Need Help??

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?

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: CUFP [id://169321]
Approved by Zaxo
[LanX]: A Team or McG ?
[Your Mother]: A-Team.
[LanX]: never liked it, but a friend of my mother was a big "fan" of Mr T xD
[LanX]: (talking about minority chicks)
[LanX]: Hollywood is built on stereotypes which sell, this includes stereotypes about English, French, russians and ... Americans
[Your Mother]: I think to some degree, maybe a large one, the stereotypes are purely products OF Hollywood and not what would sell best.
[LanX]: well those products which sold best are replicated
[Your Mother]: They get almost everything wrong on every level. I think they create and restrict the market and fundamentally misunderstand audiences.
[Your Mother]: Consider how long, for example, superhero movies were kept at bay because they weren't commercially viable. They always were, just Hollywood couldn't see it or understand how to make one because there is no management talent in the town.
[LanX]: Erich von Strohheim built his career on beeing the most hated guy (The man you love to hate)

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (9)
As of 2018-03-19 14:54 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (240 votes). Check out past polls.