Las*_*lko 9 windows command-line cmd.exe
我正在尝试使用 Windows 7 的 CMD /C 从 Perl 运行命令。该命令从提示符运行时可以正常工作,但需要引用其参数:
C:\>"C:\Program Files (x86)\gs\uninstgs.exe" "C:\Program Files (x86)\gs\gs8.63\uninstal.txt"
Run Code Online (Sandbox Code Playgroud)
没有引号,它不起作用。
如果我尝试通过 CMD /C 运行它,我发现无法强制 CMD.EXE 将带引号的字符串作为参数传递给 exe 文件。这些不起作用:
C:\>C:\Windows\System32\cmd.exe /C "C:\Program Files (x86)\gs\uninstgs.exe" "C:\Program Files (x86)\gs\gs8.63\uninstal.txt"
'C:\Program' is not recognized as an internal or external command, operable program or batch file.
C:\>C:\Windows\System32\cmd.exe /C "C:\Program Files (x86)\gs\uninstgs.exe C:\Program Files (x86)\gs\gs8.63\uninstal.txt"
'C:\Program' is not recognized as an internal or external command, operable program or batch file.
C:\>C:\Windows\System32\cmd.exe /C "C:\Program Files (x86)\gs\uninstgs.exe" \"C:\Program Files (x86)\gs\gs8.63\uninstal.txt\"
'C:\Program' is not recognized as an internal or external command, operable program or batch file.
C:\>C:\Windows\System32\cmd.exe /C "C:\Program Files (x86)\gs\uninstgs.exe" "\"C:\Program Files (x86)\gs\gs8.63\uninstal.txt\""
'C:\Program' is not recognized as an internal or external command, operable program or batch file.
C:\>C:\Windows\System32\cmd.exe /C "C:\Program Files (x86)\gs\uninstgs.exe" ""C:\Program Files (x86)\gs\gs8.63\uninstal.txt""
'C:\Program' is not recognized as an internal or external command, operable program or batch file.
C:\>C:\Windows\System32\cmd.exe /C "C:\Program Files (x86)\gs\uninstgs.exe" """C:\Program Files (x86)\gs\gs8.63\uninstal.txt"""
'C:\Program' is not recognized as an internal or external command, operable program or batch file.
C:\>C:\Windows\System32\cmd.exe /C "C:\Program Files (x86)\gs\uninstgs.exe" ^"C:\Program Files (x86)\gs\gs8.63\uninstal.txt^"
'C:\Program' is not recognized as an internal or external command, operable program or batch file.
C:\>C:\Windows\System32\cmd.exe /C "C:\Program Files (x86)\gs\uninstgs.exe" "^"C:\Program Files (x86)\gs\gs8.63\uninstal.txt^""
'C:\Program' is not recognized as an internal or external command, operable program or batch file.
Run Code Online (Sandbox Code Playgroud)
我应该使用什么语法?
有趣的是 cmd.exe 实际上包含了答案。
这是 cmd /?
If /C or /K is specified, then the remainder of the command line after
the switch is processed as a command line, where the following logic is
used to process quote (") characters:
1. If all of the following conditions are met, then quote characters
on the command line are preserved:
- no /S switch
- exactly two quote characters
- no special characters between the two quote characters,
where special is one of: &<>()@^|
- there are one or more whitespace characters between the
two quote characters
- the string between the two quote characters is the name
of an executable file.
2. Otherwise, old behavior is to see if the first character is
a quote character and if so, strip the leading character and
remove the last quote character on the command line, preserving
any text after the last quote character.
Run Code Online (Sandbox Code Playgroud)
所以在你的情况下,它将是:
C:\>C:\Windows\System32\cmd.exe /C ""C:\Program Files (x86)\gs\uninstgs.exe" "C:\Program Files (x86)\gs\gs8.63\uninstal.txt""
Run Code Online (Sandbox Code Playgroud)
也就是说,也可以使用 8.3 短名称,从而将 Program Files 截断为 Progra~1 或 Progra~2。更重要的是,您可以使用相对路径并在执行命令之前先导航到 c:\Program Files(x86)。您的命令将变为:
C:\>cd /d "c:\Program Files (x86)" && C:\Windows\System32\cmd.exe /C ".\gs\uninstgs.exe .\gs\gs8.63\uninstal.txt"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
17399 次 |
| 最近记录: |