Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Spaces in file names passed as arguments

by txixco (Novice)
on Sep 08, 2011 at 15:40 UTC ( #924842=perlquestion: print w/replies, xml ) Need Help??
txixco has asked for the wisdom of the Perl Monks concerning the following question:

Hi all.

I've got the following code to standarize my movie titles:

#!/usr/bin/perl use strict; use warnings; for (<@ARGV>) { # Change spaces to _ my $new = $_; $new =~ s/\s/_/g; rename($_, $new) or die "Cannot rename '$_' to '$new': $!"; # Capitalize my $new2 = $new; $new2 =~ s/(^|_)(\w)/$1\U$2/g; print "$_ -> $new2\n"; rename($new, $new2) or die "Cannot rename '$new' to '$new2': $!"; }

The problem is exactly because the files have spaces in the name. I've searched for a solution and I only find that I have to quote the argument; this partially works, as it works if I use something like "*.avi", but not if I use "test spacefull name.avi". Moreover, I'd like if possible to pass the file names as in the normal way, as every other command line program.

Could somebody bring me some light about how to pass file names with spaces as arguments?

Best regards.

Update: Excuse me all of you who have send me your ideas; I'm having some problems with my computer, so I will answer all of you as soon as I could try your solutions.

Replies are listed 'Best First'.
Re: Spaces in file names passed as arguments
by toolic (Bishop) on Sep 08, 2011 at 15:55 UTC
    It works for me if I get rid of the angle brackets (I'm on linux):
    for (@ARGV) {

      Thank you very much, it works :-). I thought that the only way to list files were using globbing.

Re: Spaces in file names passed as arguments
by Utilitarian (Vicar) on Sep 08, 2011 at 15:50 UTC
    Using find and double quoting the {} in an exec allows you to do the whole library in one pass
    find ./ -name \*\ \* -exec perl -e ' for (@ARGV){$new=$_;$new=~s/\s+/_ +/g;rename($_,$new);}' "{}" \;
    Works for me

    print "Good ",qw(night morning afternoon evening)[(localtime)[2]/6]," fellow monks."
Re: Spaces in file names passed as arguments
by aaron_baugher (Curate) on Sep 08, 2011 at 16:00 UTC

    If you add a line like print "Doing $_\n"; at the beginning of your for loop, you'll see that @ARGV is getting your argument already broken up into 3 parts. In other words, it's not a problem with your script, but with the shell parsing the arguments before they're passed to your script. One solution would be to escape the spaces within your quotes: "test\ spacefull\ name.avi".

    Edit: Never mind this answer; toolic caught the real problem, the brackets around @ARGV. Hard to tell what that's actually doing, but ordinarily just quoting your arguments should get them into @ARGV properly.

      Hard to tell what that's actually doing

      Not at all :)

      $ perl -MO=Deparse -le " for(<@ARGV>){print} " BEGIN { $/ = "\n"; $\ = "\n"; } use File::Glob (); foreach $_ (glob(join $", @ARGV)) { print $_; } -e syntax OK

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://924842]
Approved by Utilitarian
Front-paged by toolic
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (9)
As of 2017-01-19 20:18 GMT
Find Nodes?
    Voting Booth?
    Do you watch meteor showers?

    Results (172 votes). Check out past polls.