How to use special characters from bat file in windows
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"
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%