开发者

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

开发者 https://www.devze.com 2023-02-26 18:32 出处:网络
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

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.


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.


Try ...

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


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

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号