
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"