Nico Wawrzyniak
Nico Wawrzyniak

Reputation: 314

Save text in UTF-8 via Batch

I am still working on a converter for .m3u playlist files that ports them from a Windows Media Player generated format into a format that gets accepted by the Teamspeak 3 plugin "Soundboard".

The main converter is finished now and I encountered a last problem: When writing the new code with a Batch script it gets saved into an ANSI encoded file using echo a-lot-of-text-and-code >> 3.txt and it seems like the plugin can only open UTF-8 encoded files.

Is there any way to change the encoding of 3.txt from ANSI to UTF-8 with Batch only?

Regards, Joe

Upvotes: 1

Views: 12773

Answers (1)

rojo
rojo

Reputation: 24466

It took a bit of experimentation, but I successfully tweaked Simon Sheppard's UCS-2 encode method to encode a file as UTF-8 with batch.

@echo off
setlocal

:: utf8.bat infile outfile
:: convert infile to utf8 and save as outfile

if not exist "%~1" goto usage
if "%~2"=="" goto usage

set "infile=%~f1"
set "outfile=%~f2"

:: store current console codepage to var
for /f "tokens=2 delims=:" %%I in ('chcp') do set "_codepage=%%I"

:: temporarily change console codepage to UTF-8
>NUL chcp 65001

:: set byte order mark for outfile
>"%outfile%" set /p "=" <NUL

:: dump infile to outfile encoded as UTF-8
>>"%outfile%" type "%infile%"

:: restore console to original codepage
>NUL chcp %_codepage%

goto :EOF

:usage
echo Usage: %~nx0 infile outfile

This script itself needs to be saved in ANSI encoding, though.

Upvotes: 2

Related Questions