Use %2* in string variable in Windows batch file

浪子不回头ぞ 提交于 2019-12-10 10:36:11

问题


I'm trying to create a simple script in batch to run in Windows, the script has 3 variables:

  1. the URL to open
  2. the number of windows to open
  3. the time to wait until the new window is open

The script works fine except when the URL contains characters like %20 or %2F in which I guess cmd.exe is trying to use them as variables, I'm putting the value of the URL between quotes but that didn't help.

If I use the address:

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

the browser opens this:

https://domain.com/app.html?path=FFlocalhostFcode&do_pause=false&go=true

Is there a way I can set the variable or process its value so it does not interpret these symbols as variable names?

Thanks in advance, here is the code for reference. It is the first time I've tried to program a script in batch so any suggestion would be really appreciated.

@echo off

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

set browser="C:\Program Files\Google\Chrome\Application\chrome.exe"

echo "Running the browser..."
FOR /L %%i IN (1,1,%iterations%) DO (
%browser% %url%
timeout %time%
)

ECHO Browsers are running, press Enter when ready to close this window. . .
pause

回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/12921569/use-2-in-string-variable-in-windows-batch-file

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