Programing

배치 파일에서 앰퍼샌드를 이스케이프 처리하려면 어떻게해야합니까?

crosscheck 2020. 6. 26. 08:16
반응형

배치 파일에서 앰퍼샌드를 이스케이프 처리하려면 어떻게해야합니까?


명령을 사용 start하여 URL에 앰퍼샌드가있는 웹 페이지를 열려면 배치 파일 (또는 Windows 명령 행)에서 앰퍼샌드를 이스케이프 처리하려면 어떻게해야 합니까?

큰 따옴표는 작동하지 않습니다 start. 대신 새 명령 줄 창이 시작됩니다.

업데이트 1 : Wael Dalloul의 솔루션이 작동합니다. 또한 URL에 URL 인코딩 문자 (예 : 공백은 % 20으로 인코딩 됨) 가 있고 배치 파일에 있으면 '%'는 '%%'로 인코딩해야합니다. 이 예에서는 그렇지 않습니다.

예를 들어, 명령 행 ( CMD.EXE)에서 :

start http://www.google.com/search?client=opera&rls=en&q=escape+ampersand&sourceid=opera&ie=utf-8&oe=utf-8

결과

http://www.google.com/search?client=opera 

기본 브라우저에서 열리고 명령 행 창에서 다음 오류가 발생합니다.

'rls' is not recognized as an internal or external command,
operable program or batch file.
'q' is not recognized as an internal or external command,
operable program or batch file.
'sourceid' is not recognized as an internal or external command,
operable program or batch file.
'ie' is not recognized as an internal or external command,
operable program or batch file.
'oe' is not recognized as an internal or external command,
operable program or batch file.

플랫폼 : Windows XP 64 비트 SP2


cmd에서 :

  • &다음과 같이 이스케이프됩니다 : ^&(@Wael Dalloul의 답변을 기반으로 함 )
  • % 탈출 할 필요가 없습니다

예를 들면 :

start http://www.google.com/search?client=opera^&rls=en^&q=escape+ampersand%20and%20percentage+in+cmd^&sourceid=opera^&ie=utf-8^&oe=utf-8

배치 파일에서

  • &다음과 같이 이스케이프됩니다 : ^&(@Wael Dalloul의 답변을 기반으로 함 )
  • %다음과 같이 이스케이프됩니다 : %%(OPs 업데이트 기반)

예를 들면 :

start http://www.google.com/search?client=opera^&rls=en^&q=escape+ampersand%%20and%%20percentage+in+batch+file^&sourceid=opera^&ie=utf-8^&oe=utf-8

&명령을 분리하는 데 사용됩니다. 따라서을 ^탈출하는 데 사용할 수 있습니다 &.


더미 첫 번째 인수를 제공하면 따옴표로 묶을 수 있습니다.

이 경우 start에는 첫 번째 인수가 인용 된 경우 새 콘솔 창의 제목으로 취급되므로 더미 첫 번째 인수를 제공해야합니다 . 따라서 다음이 작동해야합니다 (여기에서 수행됨).

start "" "http://www.google.com/search?client=opera&rls=en&q=escape+ampersand&sourceid=opera&ie=utf-8&oe=utf-8"

explorer "http://www.google.com/search?client=opera&rls=...."

명령

echo this ^& that

예상대로 작동하여 출력

this & that

명령

echo this ^& that > tmp

문자열을 "tmp"파일에 쓰는 것도 가능합니다. 그러나 파이프 전에

echo this ^& that | clip

^는 완전히 다르게 해석됩니다. 두 명령 "echo this"와 "that"의 출력을 파이프에 쓰려고합니다. 에코가 작동하면 "that"이 오류를 발생시킵니다. 속담

echo this ^& echo that | clip

문자열 "this"와 "that"을 클립 보드에 넣습니다.

^없이 :

echo this & echo that | clip

첫 번째 에코는 콘솔에 기록되고 두 번째 에코의 출력 만 클립으로 파이프됩니다 ( "> tmp"리디렉션과 유사). 따라서 출력이 재 지정 될 때 ^는 &를 인용하지 않고 대신 재 지정 이전에 적용되도록합니다.

&를 파이프하려면 두 번 인용해야합니다.

echo this ^^^& that | clip

문자열을 변수에 넣으면

set m=this ^& that

그때

set m

출력합니다

m=this & that

그러나 명백한

echo %m%

Windows가 변수를 대체 한 후

echo this & that

이 명령을 새로운 명령으로 구문 분석하고 "that"을 실행하려고합니다.

In a batch file, you can use delayed expansion:

setlocal enableDelayedExpansion
echo !m!

To output to a pipe, we have to replace all &s in the variable value with ^&, which we can do with the %VAR:FROM=TO% syntax:

echo !m:^&=^^^&! | clip

On the command line, "cmd /v" enables delayed expansion:

cmd /v /c echo !m!

This works even when writing to a pipe

cmd /v /c echo !m! | clip

Simple.


If you need to echo a string that contains an ampersand, quotes won't help, because you would see them on the output as well. In such a case, use for:

for %a in ("First & Last") do echo %~a

...in a batch script:

for %%a in ("First & Last") do echo %%~a

or

for %%a in ("%~1") do echo %%~a

If you have spaces in the name of the file and you have a character you need to escape:

You can use single AND double quotes to avoid any misnomers in the command.

scp ./'files name with spaces/internal folder with spaces/"text & files stored.txt"' .

The ^ character escapes the quotes otherwise.

참고URL : https://stackoverflow.com/questions/1327431/how-do-i-escape-ampersands-in-batch-files

반응형