2

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
Harry Johnston
  • 33,445
  • 6
  • 56
  • 142
jeruki
  • 1,660
  • 3
  • 19
  • 26

2 Answers2

6

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.

Ghlitch
  • 197
  • 3
  • `%%` is used in batch files to refer local to the script variables – Serge Oct 16 '12 at 19:07
  • Did you even test before downvoting? No. Otherwise you'd see that your solution doesn't work while mine actually does. – Ghlitch Oct 16 '12 at 19:19
  • Ghlitch 1) I mentioned that I am not sure exactly because I can't check it - I have no windows around. 2) %% is used in a batch files to refer to local script variables, while the thing you have proposed works I would not recommend it to use it as it may be a source of errors if someone put it inside 'for %a' loop for example. You got your point back. Good luck – Serge Oct 16 '12 at 19:33
  • @Serge: this is the correct way to quote percent signs in a batch file. It's true that it might not work in a for loop (if the character following the quoted percent sign matches the loop variable) but there's no simple way around that (you need to use delayed expansion) and it doesn't apply in this case anyway. – Harry Johnston Oct 17 '12 at 20:51
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.

Serge
  • 5,950
  • 14
  • 27
  • i guess thats accurate but this script will have different urls all the time and im not the one who is going to run it so i cannot ask the person who will use it to scape the symbols it by hand everytime. Do you now any other way i can store it without being interpreted? – jeruki Oct 16 '12 at 19:01
  • @jeruki just a guess: are the single-quoted strings are parsed for parameter expansion as well? – Serge Oct 16 '12 at 19:06
  • @Sege the result is the same with single and double quotes – jeruki Oct 16 '12 at 20:18
  • I see. Unfortunately I don't know the other way to achieve what you need. The cmd scripting is poor compare to bash for example. But you need out of the box solution not requiring any installation. May be you consider VBscript? At least it is available in all Windows installations. – Serge Oct 16 '12 at 20:24
  • yeah i wish i could use batch, thanks i will take a look in vbs – jeruki Oct 17 '12 at 15:42
  • 1
    @jeruki: if the end user needs to input the URL, use the `set /P` command so the user can type in the URL rather than having to edit the batch script. This will avoid any need for escaping. – Harry Johnston Oct 17 '12 at 20:43
  • AFAIK there is no formal documentation for cmd.exe. However, the answers to this question may be useful: http://stackoverflow.com/questions/4094699/how-does-the-windows-command-interpreter-cmd-exe-parse-scripts – Harry Johnston Oct 18 '12 at 01:33
  • @HarryJohnston We may treat any third-party explanation as suitable. The key words are: "AFAIK there is no formal documentation for cmd.exe". And this (and many other things) made me nearly hating the MS World. But this is very subjective. – Serge Oct 18 '12 at 01:42