4

I need to open browser with special url that contains special characters (diacritics). For example "è" in url:

"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" "https://google.com/search?q=Mèxico"

I could use urlencode, e.g. "https://google.com/search?q=M%C3%A8xico" but is it possible to do without escaping because it's the user who generate the bat. Something like @this_bat_uses_utf8 or something like that. Thanks.

The following code is returning empty string:

@echo off
setlocal
set "string=Trois-Rivières, QC"
:: Define simple macros to support JavaScript within batch
set "beginJS=mshta "javascript:code(close(new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1).Write("
set "endJS=)));""
:: FOR /F does not need pipe
for /f "tokens=" %%N in (
  '%beginJS% encodeURIComponent("%string%") %endJS%'
) do set encoded=%%N
echo %string% -^> %encoded%

Example 2: this not work, remove the è and it will start working

setlocal
chcp 65001 >nul
"C:/Program Files (x86)/Google/Chrome/Application/chrome.exe" --app="https://google.com/search?q=Trois-Rivières,QC"
phuclv
  • 30,396
  • 15
  • 136
  • 260
asdjfiasd
  • 143

3 Answers3

3

There is no such directive for batch.

The most you can do is use chcp 65001 for UTF-8 and prepend a UTF-8 BOM to the .bat file.

For implementing urlencode in batch, see the script from a StackOverflow answer:

@echo off
setlocal

setlocal set "string=Trois-Rivières, QC" :: Define simple macros to support JavaScript within batch set "beginJS=mshta "javascript:code(close(new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1).Write(" set "endJS=)));"" :: FOR /F does not need pipe for /f "usebackq" %%N in ( %beginJS% encodeURIComponent('%string%') %endJS% ) do set "encoded=%%N" echo %string% -^> %encoded%

harrymc
  • 498,455
0

You can write a batch file that runs your code directly without any third party tools.

Store this code in a batch file encoded in UTF8 without BOM (when you have a BOM you should have an empty line at the top).

@echo off
setlocal

REM remeber the current code page for /f "tokens=3 delims=:. " %%d in ('chcp') do set "OLD_CP=%%d"

REM set the code page to UTF8 chcp 65001 >nul

REM do the actual work (remove the 'start ""' if you want to wait for the end of the task) start "" "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" "https://google.com/search?q=Mèxico"

REM restore the old code page (if needed) chcp %OLD_CP% >nul

Io-oI
  • 9,237
Konrad
  • 216
0
@echo off 

>nul chcp 65001 & color 
for /f delims^= %%i in ('
powershell -nOp -c """$([uri]::EscapeDataString('Mèxico'))"""
   ')do "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" "https://google.com/search?q=%%~i"

  • Porting your reference code for use with
@echo off

nul chcp 65001 & color

for /f delims^= %%i in (' powershell -nOp -c """$([uri]::EscapeDataString('Trois-Rivières, QC'))""" ')do <con: echo%%~i && set "_string=%%~i" && echo%%~i>.\My_Output_File.log

  • Output:
Trois-Rivi%C3%A8res%2C%20QC
  • Online decoded:

enter image description here


  • PowerShell one liner:
  $str="https://google.com/search?q=" + [uri]::EscapeDataString("Mèxico"); Start-Process -FilePath  "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" $str
Io-oI
  • 9,237