Use %2* in string variable in Windows batch file

蹲街弑〆低调 提交于 2019-12-06 08:59:03

Try:

set url="http://domain.com/app.html?path=%%2F%%2Flocalhost%%2Fcode&do_pause=false&go=true"

In batch files you need a %% in place of a % as %1, %2, etc are used for command line params.

I am not sure completely - it was long time ago. Try this please:

set url="http://domain.com/app.html?path=^%2F^%2Flocalhost^%2Fcode&do_pause=false&go=true"

Ok, finally I found where did I get about caret (^) escaping and what should be escaped with caret:

Fourth, all reserved shell characters not in double quotes must be escaped. These characters have special meaning to the Windows NT command shell. The reserved shell characters are:

& | ( ) < > ^

To pass reserved shell characters as part of an argument for a command, either the entire argument must be enclosed in double quotes, or the reserved character must be escaped. Prefix a reserved character with a carat (^) character to escape it. For example, the following command example will not work as expected, because < and > are reserved shell characters:

The source (http://technet.microsoft.com/en-us/library/cc723564.aspx) apparently is a good reading on some not clearly defined things in CMD scripting collected in one document. However it does not describe the '%' quoting. Nevertheless I found one place that mentions the %% case but not in a general context:

Use of CALL to expand a line a second time.
set var=one
(
  set var=two
  echo %var%
  call echo %%var%%
)

Only the second call echo will output two, as the parser will first parse the 
line when parsing the block to call echo %var%, as doubled percents are reduced
to one.

Source: http://technet.microsoft.com/en-us/library/cc732835(v=ws.10).aspx

However the whole official Command Line Reference is replete with words 'it seems like':

Reparsing of parenthesis blocks also fails with strange error messages, it seems 
to discover some parser/token internals. 
call (echo one ^| echo two)

So, my impression that no one actually knows or even tried to define a more or less structurized syntax and behavior of the cmd shell.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!