Chris Nelson
Chris Nelson

Reputation: 3707

How do I start a Windows program with spaces in the path from Perl?

If I do:

my program = "C:\\MyPath\\MyProg.exe";
system(("start", $program));

MyProg starts up just fine and my script resumes after the system() command. But if there are spaces in the path like

my program = "C:\\My Path\\MyProg.exe";
system(("start", $program));

It seems to run cmd, not MyProg.

I've tried quoting with things like:

my program = "C:\\My Path\\MyProg.exe";
system(("start", '"' . $program . '"'));

But nothing seems to help.

Of course I can get around it with fork() but I'd like to understand why I can't pass a path with spaces as an argument.

Upvotes: 2

Views: 3427

Answers (3)

Joshua
Joshua

Reputation: 8212

That's because the built-in start command is a bit weird when it comes to quotes. You can reproduce this on the command line with start "C:\My Path\MyProg.exe" and see the same result. To properly execute it you need a set of empty quotes before it: start "" "C:\My Path\MyProg.exe".

So your end result should be:

my program = "C:\\My Path\\MyProg.exe";
system('start "" "' . $program . '"');

Edited to include the suggesstion from ikegami. My perl is a bit rusty as I haven't used it in years.

Upvotes: 3

istudy0
istudy0

Reputation: 1333

I am not an perl expert but I found the following link.

http://bytes.com/topic/perl/answers/697488-problem-system-command-windows

Upvotes: 0

Brian Roach
Brian Roach

Reputation: 76908

Try ...

my program = "C:/\"My Path\"/MyProg.exe";

Upvotes: 0

Related Questions