0

I have two scenarios in which I want to open multiple URLs that have special characters: - and ?. In the first, I have tried escaping occurrences of - with ^, but the _sff# variables all throw errors.

:Item_1
set _base=https://www.~.com/ & _mth1=2019-12 & _mth2=2020-01 & _week=2001 & _sff1=LNI & _sff2=NI & _sff3=NI-final & _sff4=NI-reduced
start brave --incognito %_base%%_mth1%/path-x/NW%_week%-%_sff1%.pdf & ping localhost -n 2 > nul
start brave --incognito %_base%%_mth1%/path-x/NW%_week%-%_sff2%.pdf & ping localhost -n 2 > nul
start brave --incognito %_base%%_mth1%/path-x/NW%_week%-%_sff3%.pdf & ping localhost -n 2 > nul
start brave --incognito %_base%%_mth1%/path-x/NW%_week%-%_sff4%.pdf & ping localhost -n 2 > nul
start brave --incognito %_base%%_mth2%/path-x/NW%_week%-%_sff1%.pdf & ping localhost -n 2 > nul
start brave --incognito %_base%%_mth2%/path-x/NW%_week%-%_sff2%.pdf & ping localhost -n 2 > nul
start brave --incognito %_base%%_mth2%/path-x/NW%_week%-%_sff3%.pdf & ping localhost -n 2 > nul
start brave --incognito %_base%%_mth2%/path-x/NW%_week%-%_sff4%.pdf
goto :EOF

In this scenario, how can I handle the ??

:Item_2
set _urls=https://www.~.com/?a=b
set _urls=%urls%;https://www.~.com/
call :Browser
goto :EOF

:Browser
for %%a in (%_urls%) do (
    start browser --param %%a & ping localhost -n 2 > nul
)
exit /b
user2319146
  • 329
  • 1
  • 10
  • 2
    Open a [command prompt](https://www.howtogeek.com/235101/), run `cmd /?` and read the output help. There is explained that a file name containing a space or one of these characters ``&()[]{}^=;!'+,`~`` must be enclosed in double quotes to get all these characters interpreted as literal characters. That is also true for any other argument string passed to a command or program which could contain in string additionally the characters `<>|?:*` which are not allowed in the name of a file. Start with `set "_urls=https://www.~.com/?a=b"`, continue with `set "_urls=%urls%;https://www.~.com/"`, etc. – Mofi Jan 09 '20 at 18:14
  • See also: [Why is no string output with 'echo %var%' after using 'set var = text' on command line?](https://stackoverflow.com/a/26388460/3074564) In your case it is necessary to assign double quoted urls to the environment variable, i.e. use `set "_urls="https://www.~.com/?a=b""` and `set "_urls=%urls% "https://www.~.com/""`. Then you can use `for %%I in (%_urls%) do start "" browser --param %%I & %SystemRoot%\System32\ping.exe localhost -n 2 >nul` – Mofi Jan 09 '20 at 18:20
  • It would be also a good idea to read [Single line with multiple commands using Windows batch file](https://stackoverflow.com/a/25344009/3074564). In first batch file code there must be `set` used left to all environment variables which should be defined on a single line without really knowing why, i.e. use `set "_base=https://www.~.com/" & set "_mth1=2019-12" & set "_mth2=2020-01" & set "_week=2001" & set "_sff1=LNI" & set "_sff2=NI" & set "_sff3=NI-final" & "_sff4=NI-reduced"`. – Mofi Jan 09 '20 at 18:28
  • And the next line should be `start "" brave --incognito "%_base%%_mth1%/path-x/NW%_week%-%_sff1%.pdf" & %SystemRoot%\System32\ping.exe localhost -n 2 >nul` for a 100% valid command line working for any PDF file name. – Mofi Jan 09 '20 at 18:29
  • The `?` is a wildcard that is recognised by `for`, and you cannot escape it; the `-` is nothing special, but `&` is, so use `^&` to escape it or quote the string with `""`... – aschipfl Jan 09 '20 at 22:03
  • To solve the issue with `?` you could replace them by a character forbidden in URLs (like `|` or \`) and replace it back in the loop afterwards; or you use a `for /F "delims="` loop that reads a text file that contains the URLs line by line... – aschipfl Jan 09 '20 at 22:24
  • There are lots of other and most likely better approaches than what you like to do which avoids that `?` is interpreted as wildcard character by `for`. But if you want to keep this approach, you can defined the first url with `set "_urls="https://www.~.com/%%3Fa=b""`. The question mark is [percent encoded](https://url.spec.whatwg.org/#percent-encoded-bytes) in the url which is not necessary for `?`, but can be nevertheless done. `%` must be escaped in a batch file with `%` to be interpreted as literal percent character. However, the usage of a different method would make the code much easier. – Mofi Jan 10 '20 at 07:06

0 Answers0