Skip to content

Instantly share code, notes, and snippets.

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

Convert files from UTF-16LE to ANSI.

This gist is released under GLWTPL.

type Unicode2ANSI /? for detailed help.

@echo off
setlocal enableextensions
if "%~1" == "/?" (
echo Convert files from ANSI to Unicode
echo
echo Syntax: ANSI2Unicode codepage inputdirectory [outputdirectory]
echo/
echo codepage is the code page corresponding to the encoding of the text files that are converted to.
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 is defaulted to inputdirectory_ANSI.
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_ANSI"
) else (
set "_OUTPUTDIRECTORY=%~3"
)
md "%_OUTPUTDIRECTORY%" 2> nul
%ComSpec% /a /c Unicode2ANSISub %1 %2 "%_OUTPUTDIRECTORY%"
if errorlevel 1 (
echo Invalid code page.
rd /s /q "%_OUTPUTDIRECTORY%"
goto :eof
)
echo Files in "%~2" are converted from Unicode to code page %~1 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