无法在 Windows 的命令外壳中转义空间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8260979/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 18:31:26  来源:igfitidea点击:

unable to escape space in command shell of Windows

windowscommand-lineescapingcommandcommand-line-arguments

提问by user837208

If I execute

如果我执行

cmd /c "%programfiles%\mycode\md5sums.exe" %temp%

cmd /c "%programfiles%\mycode\md5sums.exe" %temp%

it works just fine. But when I execute

它工作得很好。但是当我执行

cmd /c "%programfiles%\mycode\md5sums.exe" %programfiles%

cmd /c "%programfiles%\mycode\md5sums.exe" %programfiles%

I get below error-

我得到以下错误-

Error: Unable to read file/directory C:\Program

Error: Unable to read file/directory C:\Program

which means that md5sums.exe is trying to open C:\Programas opposed to full path given by %programfiles%

这意味着 md5sums.exe 正在尝试打开C:\Program而不是 %programfiles% 给出的完整路径

I've to use cmd /cas I've to run this command remotely.

我必须使用,cmd /c因为我必须远程运行此命令。

How do I make it work? I've tried using `"%programfiles%" but in this case even md5sums.exe is not getting executed.

我如何使它工作?我试过使用 `"%programfiles%" 但在这种情况下,甚至 md5sums.exe 也没有被执行。

Eventually I would like to use switch "/b" provided by md5sums.exe but at this point of time I am stuck at even making md5sums run on %programfiles%

最终我想使用 md5sums.exe 提供的开关“/b”,但此时我什至无法让 md5sums 在 %programfiles% 上运行

回答by Marc B

Try:

尝试:

cmd /c "%programfiles%\mycode\md5sums.exe" "%programfiles%"
                                           ^--------------^--quotes added

Otherwise, you end up with something like:

否则,你最终会得到类似的结果:

cmd /c "C:\Program Files\mycode\md5sums.exe" C:\Program Files

and end up passing two arguments to md5sums, not a single path.

并最终将两个参数传递给 md5sums,而不是单个路径。

回答by mausi

you need a double double quote at start and end e.g.:

您需要在开始和结束时使用双双引号,例如:

cmd /c ""%programfiles%\mycode\md5sums.exe" "%programfiles%""

cmd /c ""%programfiles%\mycode\md5sums.exe" "%programfiles%""