Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have a script that needs to copy a file that has blank spaces in the file name. example: virtualmachine 6 2.vmx How can I escape those blank spaces?

Replies are listed 'Best First'.
Re: Escape blank spaces
by doom (Deacon) on Jun 19, 2007 at 00:07 UTC
    This definitely works:
    use File::Copy qw(copy move); my $filename1 = "/tmp/blah blah blah.txt"; my $filename2 = "/tmp/TargetLocation/blah blah blah.txt"; copy( $filename1, $filename2);

    At a guess you were trying to shell out to "cp", right?

    You might try looking at: String::ShellQuote

Re: Escape blank spaces
by Zaxo (Archbishop) on Jun 19, 2007 at 00:02 UTC

    That depends on whether a shell will interpret the command line that includes the file name. If it will, quotemeta should do it, along with handling lots of other gotchas.

    $ perl -e'$_ = "foo bar baz"; print quotemeta, $/'
    foo\ bar\ baz
    $ 
    

    If there is no shell interpretation, you probably don't need to escape them.

    After Compline,
    Zaxo

Re: Escape blank spaces
by ff (Hermit) on Jun 19, 2007 at 02:04 UTC
    An end-around for handling "wacky" filename issues on Win32 is to resort to:

    use Win32; use strict; my $filename_w_spaces = 'this err.txt'; # the file has to exist to play this game.... if ( -e $filename_w_spaces ) { print "$filename_w_spaces\n"; my $short_filename_wo_spaces = Win32::GetShortPathName($filename_w_spaces); print "$short_filename_wo_spaces\n"; my $long_filename_w_spaces = Win32::GetLongPathName($short_filename_wo_spaces); print "$long_filename_w_spaces\n"; }
      It's my understanding that generation of short names can be turned off, so it's not good to count on them.
        Is that an option the Win32 user sets such that programs coming along that use short names get stymied?