Convert files from ANSI to UTF-16LE.
This gist is released under GLWTPL.
type ANSI2Unicode /?
for detailed help.
!ANSI2Unicode |
@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 |