Why does windows START command not work with spaces in arguments AND path?

元气小坏坏 提交于 2019-12-21 04:42:15

问题


This command works

START /b /wait "Dummy title" "C:\tmp\test runner2.bat" arg1 arg2

But both of these fails!

START /b /wait "Dummy title" "C:\tmp\test runner2.bat" arg1 arg2 "arg 3" arg4
START /b /wait "Dummy title" "C:\tmp\test runner2.bat" arg1 arg2 "arg 3"

The error is:

'C:\tmp\test' is not recognized as an internal or external command, operable program or batch file.

Obviously it has something to do with " surounding the arguments, but why and how do I work around this?

Related questions:

  • How to create batch file in Windows using “start” with a path and command with spaces
  • Can I use the “start” command with spaces in the path?

回答1:


It's a known bug of the START command.
If you have spaces in both, the command and of the parameters and try to handle them with quotes, it fails.

First the START command check if the full command exists.
But then it starts only the first part.

In your case it looks for "C:\tmp\test runner2.bat" but try to start C:\tmp\test.

You can avoid it when the command is replaced by a CALL

START /b /wait "Dummy title" CALL "C:\tmp\test runner2.bat" arg1 arg2 "arg 3" arg4

The START uses cmd /k to start the new process.
And that is the cause for the misbehaviour.
Paul Groke mentioned the fact, that this only occours when it's a batch file.
Exe files will be executed directly and so they are not affected by the cmd.exe bug.

In your case

C:\Windows\system32\cmd.exe  /K "C:\tmp\test runner2.bat" arg1 arg2 "arg 3" arg4

And the help of cmd /k and cmd /c explains, that in this case the first and last quote are removed.




回答2:


"Jeb" already pointed into the right direction. In my case i didn't tried to run a batch, but a program in the "Program Files" folder (batch shall terminate after launching the program). When calling

START "C:\Program Files\MyAppPath\MyApp.exe" arg1 arg2 ... argN

the path typed with quotes around is supposed to be the "Title" parameter by START command. To get rid of that, you have to "fake" a window title like that:

START "" "C:\Program Files\MyAppPath\MyApp.exe" arg1 arg2 ... argN

This helped in my case.




回答3:


This does not answer my question, but it does solve the immediate problem I'm having.

While reading through the "Problem with quotes around file names in Windows command shell"-post I discovered a workaround:

cmd.exe /C ""C:\tmp\test runner2.bat" arg1 arg2 "arg 3" arg4"

There is also the other workaround by simply executing the command with the call command instead (as stated by Ansgar Wiechers)

call "C:\tmp\test runner2.bat" arg1 arg2 "arg 3" arg4


来源:https://stackoverflow.com/questions/17674255/why-does-windows-start-command-not-work-with-spaces-in-arguments-and-path

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!