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