3

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"
2

3 Answers 3

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%
2
  • You can modify the behavior of the for /f command by adding the tokens= parameter to exclude blanks as separators.
    – harrymc
    Sep 17, 2021 at 7:38
  • There was a small error in the StackOverflow answer, which I fixed above (with your example data). Blanks are not a problem, since they are converted to the non-blank %20, so tokens= is finally not required. I have tested the example and it works for me.
    – harrymc
    Sep 17, 2021 at 16:21
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
2
  • This doesn't work for backward long e, see example 2
    – asdjfiasd
    Sep 17, 2021 at 14:17
  • 1
    Indeed it does work (see comment for example 2). UTF8 is full Unicode character set. When one character like 'è' works, any other character should work too.
    – Konrad
    Sep 20, 2021 at 8:34
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.