批量中将文件编码从UTF16更改为UTF8

时间:2015-01-26 10:54:59

标签: batch-file utf-8

我有一些UTF16 Big Endian格式的CSV文件。我需要编写一个批处理脚本来将其转换为UTF8。

没有外部程序,或者OS附带的外部程序(因此总是可以预期在那里),是否可以这样做?

1 个答案:

答案 0 :(得分:0)

不确定。只需用UTF-8 BOM替换UTF-16 BE BOM。

@echo off
setlocal

set "csvfile=test.csv"

:: 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 1252

<"%csvfile%" set /p "firstLine="

setlocal enabledelayedexpansion
>fixed.csv echo(!firstLine:*ÿ=!
endlocal

for /f "usebackq skip=1 delims=" %%I in ("%csvfile%") do (
    >>fixed.csv echo(%%I
)

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

>NUL move /y fixed.csv "%csvfile%"

echo Fixed.

I'll leave it as an exercise for the O.P. to make this loop through multiple CSV files.