Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Spaces in file names passed as arguments

by txixco (Novice)
on Sep 08, 2011 at 15:40 UTC ( [id://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 titulize.pl "*.avi", but not if I use titulize.pl "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: titulize.pl "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
      glob

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://924842]
Approved by Utilitarian
Front-paged by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (5)
As of 2024-04-19 23:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found