Skip to content

Instantly share code, notes, and snippets.

@jay
Last active January 17, 2021 07:04
Show Gist options
  • Save jay/731030525b58ae3845b7ba59c0757b3c to your computer and use it in GitHub Desktop.
Save jay/731030525b58ae3845b7ba59c0757b3c to your computer and use it in GitHub Desktop.
Example of a UTF-8 encoded batch file.
:: Example of a UTF-8 encoded batch file.
::
:: Usage: utf8_encoded_example.bat
::
:: Ref: https://github.com/curl/curl/issues/6386#issuecomment-761745450
::
:: Public Domain: No License. Written by Jay Satiro <[email protected]>
::
:: https://gist.github.com/jay/731030525b58ae3845b7ba59c0757b3c
::
@echo off
setlocal
set EXITCODE=
REM Save the current code page then change to UTF-8 code page.
set OLDCP=
for /f "tokens=2 delims=:" %%A in ('chcp') do set "OLDCP=%%~A"
for /f %%A in ("%OLDCP%") do set "OLDCP=%%~A"
echo "%OLDCP%"| findstr /r "^\"[0123456789][0123456789]*\"$" 1>NUL 2>&1
if %ERRORLEVEL% NEQ 0 echo Fatal: Failed parsing code page id. & exit /b 1
chcp 65001 1>NUL
if %ERRORLEVEL% NEQ 0 echo Fatal: Failed changing code page id. & exit /b 1
REM All the UTF-8 command lines come after this comment. Now that the code page
REM has been changed do not use the exit command or goto :EOF or premature
REM termination (CTRL+C) to exit the batch file; instead goto quit to restore
REM old code page.
echo спасти>спасти.txt
type спасти.txt
:quit
REM Restore the old code page.
chcp %OLDCP% 1>NUL
exit /b %EXITCODE%
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment