как экранировать " в параметрах cmd.exe /c?

как экранировать " в параметрах cmd.exe /c?

Я пытаюсь запустить команду из Perl, используя CMD /C в Windows 7. Команда, запущенная из командной строки, работает нормально, но ее параметры нужно заключить в кавычки:

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 и таким образом обрезать 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"

Связанный контент