Skip to content

Instantly share code, notes, and snippets.

@seiren-naru-shirayuri
Last active April 1, 2022 14:58
Show Gist options
  • Save seiren-naru-shirayuri/fd0d174ba0b76d3a4c139ae477ea9a6a to your computer and use it in GitHub Desktop.
Save seiren-naru-shirayuri/fd0d174ba0b76d3a4c139ae477ea9a6a to your computer and use it in GitHub Desktop.
Convert files from ANSI to UTF-16LE

Convert files from ANSI to UTF-16LE.

This gist is released under GLWTPL.

type ANSI2Unicode /? for detailed help.

@rem THE ENCODING OF THIS FILE IS WINDOWS-1252!!!
@echo off
setlocal enableextensions
if "%~1" == "/?" (
echo Convert files from Unicode to ANSI
echo
echo Syntax: ANSI2Unicode codepage inputdirectory [outputdirectory]
echo/
echo codepage is the code page corresponding to the encoding of the text files to be converted.
echo inputdirectory is the directory where text files to be converted are.
echo outputdirectory is the directory where converted text files are stored in.
echo If outputdirectory is omitted, the outputdirectory is defaulted to inputdirectory_Unicode.
goto :eof
)
if [%1] == [] (
echo Missing parameter code page.
goto help
)
if [%2] == [] (
echo Missing parameter inputdirectory.
goto help
) else (
if not exist "%~2" (
echo Directory "%~2" not found.
goto :eof
)
)
if [%3] == [] (
set "_OUTPUTDIRECTORY=%~2_Unicode"
) else (
set "_OUTPUTDIRECTORY=%~3"
)
for /f "tokens=2 delims=:" %%I in ('chcp') do set "_OLDCODEPAGE=%%I"
md "%_OUTPUTDIRECTORY%" 2> nul
chcp 1252 > nul
for %%I in ("%~2\*") do set /p=ÿþ<nul > "%_OUTPUTDIRECTORY%\%%~nxI"
chcp %_OLDCODEPAGE% > nul
%ComSpec% /u /c ANSI2UnicodeSub %1 %2 "%_OUTPUTDIRECTORY%"
if errorlevel 1 (
echo Invalid code page.
rd /s /q "%_OUTPUTDIRECTORY%"
goto :eof
)
echo Files in "%~2" are converted from code page %~1 to Unicode and saved in "%_OUTPUTDIRECTORY%".
@echo off
setlocal enableextensions
setlocal
for /f "tokens=2 delims=:" %%I in ('chcp') do set "_OLDCODEPAGE=%%I"
chcp %~1 > nul 2>&1
if errorlevel 1 exit 1
for %%I in ("%~2\*") do type "%%~I" >> "%~3\%%~nxI"
chcp %_OLDCODEPAGE% > nul
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment