How do I access paths with spaces in them in Perl on Windows?

12,304

Solution 1

You are quoting the entire command, including the command-line arguments. You should have put your second escaped quote after the mysql.exe:

open (IN, "| \"C:\\Program Files\\MySQL\\MySQL Server 5.1\\bin\\mysql.exe\" -u root -ppwd") or die "$!";

You might also be interested in the qq() and q() operators, which allow you to use delimiters other than quotation marks to delimit strings. They are very helpful when you want to quote a string that includes quotes:

qq[| "C:\\Program Files\\MySQL\\MySQL Server 5.1\\bin\\mysql.exe" -u root -ppwd]

Also, Perl will happily handle the correct path separator for command names (but not always for command arguments, so beware):

qq[| "C:/Program Files/MySQL/MySQL Server 5.1/bin/mysql.exe" -u root -ppwd]

(And since this example doesn't need any interpolation, you could have used single-quotes or the q() construction:

'| "C:\\Program Files\\MySQL\\MySQL Server 5.1\\bin\\mysql.exe" -u root -ppwd'

)

Solution 2

You have to escape the spaces as well.

open (IN, "| C:\\Program\ Files\\MySQL\\MySQL\ Server\ 5.1\\bin\\mysql.exe -u root -ppwd") or die "$!";

Or use the old 8.3 names:

open (IN, "| C:\\Progra~1\\MySQL\\MySQL~1\\bin\\mysql.exe -u root -ppwd") or die "$!";

Though, I have to question the sanity of piping the MySQL client, instead of just using DBI

Solution 3

It's Perl, so there are a 1000 ways (as you'll see), One way (escape the spaces)

open (IN, "| C:\\Program\ Files\\MySQL\\MySQL\ Server\ 5.1\\bin\\mysql.exe -u root -ppwd") or die "$!";

Solution 4

My solution was to do this:

$mysql = "C:\\Program\ Files\\MySQL\\MySQL\ Server\ 5.1\\bin\\mysql.exe";
open (IN, "| \"$mysql\" -u root -ppwd") or die "$!";

Update: I also noticed that as @mob rightfully points out I had my \" in the wrong place. Twenty five years of DOS and I miss that :/

Share:
12,304
Kev
Author by

Kev

###Actively looking for freelance work ###About Me: I'm a professional software developer and have spent my time building provisioning and web based self-service systems for IIS, Apache and Citrix XenServer, amongst other things. My Curriculum Vitae can be viewed on Stack Overflow Careers (might be a bit out of date). Stuff I like to listen to at last.fm You can get in touch here: kevin.e.kenny #@# gmail.com (you know what to do with the # and spaces). No Survey Emails Please. Also not ashamed to admit I like trains, mostly diesels, late Era 8 (BR Sectorisation) and Era 9 onwards :) I'm also interested in signalling if anyone from Network Rail is looking this far down ;)

Updated on June 04, 2022

Comments

  • Kev
    Kev about 2 years

    I'm converting a Unix Perl script to run on Windows. I'm having a problem with paths that have spaces in them:

    open (IN, "| C:\\Program Files\\MySQL\\MySQL Server 5.1\\bin\\mysql.exe -u root -ppwd") or die "$!";
    

    The code above throws the following error:

    'C:\Program' is not recognized as an internal or external command,

    I tried wrapping in escaped \" like this:

    open (IN, "| \"C:\\Program Files\\MySQL\\MySQL Server 5.1\\bin\\mysql.exe -u root -ppwd\"") or die "$!";
    

    But no joy. How do I handle paths with spaces?

    I'm using ActiveState v5.10.0 built for MSWin32-x86-multi-thread.