如何轉義cmd.exe /c參數中的“?

如何轉義cmd.exe /c參數中的“?

我正在嘗試使用 Windows 7 的 CMD /C 從 Perl 運行命令。該命令在從提示符號運行時工作正常,但需要引用其參數:

C:\>"C:\Program Files (x86)\gs\uninstgs.exe" "C:\Program Files (x86)\gs\gs8.63\uninstal.txt"

如果沒有引號,它就不起作用。

如果我嘗試透過 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.

我應該使用什麼語法?

答案1

有趣的是 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.

所以在你的情況下它將是:

C:\>C:\Windows\System32\cmd.exe /C ""C:\Program Files (x86)\gs\uninstgs.exe" "C:\Program Files (x86)\gs\gs8.63\uninstal.txt""

也就是說,也可以使用 8.3 短名稱,從而將程式檔案截斷為 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"

相關內容