cmd.exe를 사용할 때 따옴표를 어떻게 처리합니까?
나는 시도하고있다 :
cmd.exe /C "C:\Program Files\Somewhere\SomeProgram.exe" > "C:\temp\Folder Containing Spaces\SomeProgram.out"
그러나 cmd.exe가 작동하는 방식에 문제가 있습니다. 도움말을 계속해서 "문자를 특별한 방식으로 처리합니다. 질문 끝에 도움말을 참조하십시오. 따라서 여기서 실행되지 않음 ... cmd.exe가 따옴표를 제거하여 잘못되었습니다.
이 작업을 수행 할 수 있습니다.
// quotes not required around folder with no spaces
cmd.exe /C "C:\Program Files\Somewhere\SomeProgram.exe" > C:\temp\FolderWithNoSpaces\SomeProgram.out
그러나 나는 일할 첫 번째 사람이 필요합니다. cmd.exe가 사용하는 이상한 따옴표 처리 주변에 있습니까? 모든 따옴표를 보존하고 싶지만 그렇게 할 수있는 옵션이없는 것입니다.
출력에서 제품 도움말 : cmd /?
/ C 또는 / K가 지정 널 스위치 이후의 나머지 명령 줄은 명령 줄로 처리됩니다. 여기에서 따옴표 ( ") 문자를 처리하는 데 다음 논리가 사용됩니다.
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
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.
아. doh. 내 질문에 답 많이 생각합니다.
/ S를 사용하고 전체를 따옴표로 묶으면 외부 따옴표 만 제거됩니다.
cmd.exe /S /C " do what you like here, quotes within the outermost quotes will be preserved "
나는 당신의 예제가 그대로 있다는 것을 알고 있습니다.
cmd.exe /C "C:\Program Files\Somewhere\SomeProgram.exe" > "C:\temp\Folder Containing Spaces\SomeProgram.out"
http://pastebin.com/raw.php?i=YtwQXTGN 여기에 귀하의 예를 재현했습니다 .
C:\>cmd /c "c:\Program Files\my folder\my long program.exe" > "c:\temp\spaces are here\a.a"
C:\>type "c:\temp\spaces are here\a.a"
my long program.exe has run
C:\>
further example demonstrating it works with "my long program.exe", removing cmd /c, it operates fine too.
C:\>"c:\Program Files\my folder\my long program.exe" > "c:\temp\spaces are here\
a.a"
C:\>type "c:\temp\spaces are here\a.a"
my long program.exe has run
C:\>
Another example, but with replace. replace with no parameters says "source path required" "no files replaced"
C:\>replace > a.a
Source path required
C:\>type a.a
No files replaced
Exactly the same effect when they're in folders with spaces.
C:\>cmd /c "c:\Program Files\my folder\replace.exe" > "c:\temp\spaces are here\r.r"
Source path required
C:\>type "c:\temp\spaces are here\r.r"
No files replaced
C:\>
further demonstration with replace
without cmd /c works fine too.
C:\>"c:\Program Files\my folder\replace.exe" > "c:\temp\spaces are here\r.r"
Source path required
C:\>type "c:\temp\spaces are here\r.r"
No files replaced
C:\>
귀하의 예제가 잘 작동하는 이유
cmd.exe /C "C:\Program Files\Somewhere\SomeProgram.exe" > "C:\temp\Folder Containing Spaces\SomeProgram.out"
그리고 그것이 작동하는 방식과 이유는>가 host.exe에 의해 특별하게 해석되기 때문입니다. 그래서이 부분은 -제 생각에- 먼저 cmd.exe /C "C:\Program Files\Somewhere\SomeProgram.exe"
평가됩니다. 즉, cmd / c는> 및 이후를 볼 수 있습니다.
cmd /? 2 건
사례 1 및 사례 2. 귀하의 예는 사례 1에 적합합니다.
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.
예제가 사례 1에 맞는지 테스트 할 수 있습니다. 왜냐하면 / s를 추가하면 (/ s를 추가하는 것 외에 다른 예제를 변경하거나 따옴표를 추가하지 않고) 다른 결과를 얻을 수 있기 때문입니다. 귀하의 사례가 사례 1이라는 것을 증명합니다. 사례 1의 모든 기준을 분명히 충족합니다. 사례가 사례 2이고 / s를 추가하면 아무런 차이가 없습니다. .
당신의 대답은 당신의 결과를 얻는 다른 방법을 보여주기 때문에 흥미 롭습니다. 그러나 경우 2의 경우에는 추가 외부 따옴표를 추가하고 / s를 추가합니다.
그러나 실제로 이러한 추가 외부 따옴표를 추가하면 케이스 2로 만들었으며 그 위에 / s를 추가해도 차이가 없습니다.
C:\>cmd /c "c:\Program Files\my folder\replace.exe"
Source path required
No files replaced
C:\>cmd /s /c "c:\Program Files\my folder\replace.exe"
'c:\Program' is not recognized as an internal or external command,
operable program or batch file.
C:\>cmd /c ""c:\Program Files\my folder\replace.exe""
Source path required
No files replaced
C:\>cmd /s /c ""c:\Program Files\my folder\replace.exe""
Source path required
No files replaced
C:\>
질문의 예가 잘 작동했습니다.
cmd.exe /C "C:\Program Files\Somewhere\SomeProgram.exe" > "C:\temp\Folder Containing Spaces\SomeProgram.out"
예제를 작동시키기위한 대답으로 제공하는 대안 (/ S 및 외부 따옴표 포함)도 잘 작동합니다.
cmd.exe /S /C ""C:\Program Files\Somewhere\SomeProgram.exe" > "C:\temp\Folder Containing Spaces\SomeProgram.out""
대안 인 귀하의 대답은 이미 사례 2이기 때문에 / S를 제거하여 실제로 단순화 할 수 있으므로 / s를 추가해도 아무런 차이가 없습니다. 그래서 이것은 당신의 대답에 주어진 해결책을 향상시킬 것입니다.
cmd.exe /C ""C:\Program Files\Somewhere\SomeProgram.exe" > "C:\temp\Folder Containing Spaces\SomeProgram.out""
질문에서 문제로 설명한 예와 솔루션은 동일한 좋은 결과를 생성합니다. 그러나 한 가지 큰 차이점이 있다고 생각합니다 (그리고 그것을 테스트하는 방법을 모르겠습니다).하지만 귀하의 예제가 작동하는 방식과 답변의 솔루션이 작동하는 방식의 한 가지 차이점은 귀하의 예제의 경우라고 생각합니다. 호스팅 / 호출 cmd.exe는 파일에 대한 리디렉션을 수행합니다. 솔루션의 예에서 호출 된 cmd.exe는 호스트 cmd.exe에 의해> 전달되므로 호출 된 cmd.exe가 리디렉션을 수행합니다. 물론 귀하의 사례는 사례 1이고 솔루션은 사례 2에서 작동하도록 (매우 잘) 수정 한 것입니다.
내가 여기서 실수하지 않았 으면 좋겠어. 그러나 귀하의 질문과 답변은 cmd, 특히 cmd / c가 어떻게 작동하는지에 대해 머리를 감싸는 데 도움이되었습니다!
아마도 귀하의 예는 실제 예를 지나치게 단순화했으며 실제 예는 실패하여 수정이 필요했습니다. 예를 들어 따옴표를 사용하는 프로그램에 대한 매개 변수를 갖는 등의 예제 사례가 조금 더 복잡했다면 사례 1이 실패하고 실제로 외부 따옴표가 필요합니다 (/ S는 결과를 변경하지 않습니다 , 따라서 필요한 외부 따옴표를 추가하면 이미 사례 2이므로 / S가 필요하지 않습니다. 그러나 귀하의 질문에 제시 한 예는 실제로 잘 작동하는 것 같습니다.
추가됨 -관련 Q 및 A`cmd / s` 란 무엇입니까?
참조 URL : https://stackoverflow.com/questions/355988/how-do-i-deal-with-quote-characters-when-using-cmd-exe
'ProgramingTip' 카테고리의 다른 글
ansible 플레이 북 파일에 ssh 키 지정 (0) | 2020.12.30 |
---|---|
'None is None is None'이 True를 반환하는 이유는 무엇입니까? (0) | 2020.12.30 |
UI의 무단 작업을 숨기거나 수행해야합니까? 아니면 오류가 발생합니까? (0) | 2020.12.30 |
Maven은 SSH를 사용하여 로컬 파일을 원격 서버에 복사합니다. (0) | 2020.12.30 |
int []를 byte []로 변환하는 방법 (0) | 2020.12.30 |