csv文件UTF-8(带BOM)到ANSI / Windows-1251

时间:2016-08-08 18:11:03

标签: windows batch-file utf-8 vbscript codepages

我希望创建一个批处理文件/宏来删除自动生成的UTF-8 CSV的第一行,并将其转换为Windows代码页1251(" ANSI")。 我一直在互联网上寻找并尝试过很多东西,但却无法找到有效的东西......

删除第一行很简单

@echo off
set "csv=test.csv"
more +1 "%csv%" >"%csv%.new"
move /y "%csv%.new" "export\%csv%" >nul
之后我失去了,我尝试使用DOS中的TYPE设置

cmd /a /c TYPE test.csv > ansi.csv

和许多变种,但它返回一个空的CP1251文件或只是另一个UTF文件。

我尝试过使用vbs,但这又返回了另一个UTF-8文件,但现在没有BOM

Option Explicit

Private Const adReadAll = -1
Private Const adSaveCreateOverWrite = 2
Private Const adTypeBinary = 1
Private Const adTypeText = 2
Private Const adWriteChar = 0

Private Sub UTF8toANSI(ByVal UTF8FName, ByVal ANSIFName)
    Dim strText

    With CreateObject("ADODB.Stream")
        .Open
        .Type = adTypeBinary
        .LoadFromFile UTF8FName
        .Type = adTypeText
        .Charset = "utf-8"
        strText = .ReadText(adReadAll)
        .Position = 0
        .SetEOS
        .Charset = "_autodetect" 'Use current ANSI codepage.
        .WriteText strText, adWriteChar
        .SaveToFile ANSIFName, adSaveCreateOverWrite
        .Close
    End With
End Sub

UTF8toANSI "UTF8-wBOM.txt", "ANSI1.txt"
UTF8toANSI "UTF8-noBOM.txt", "ANSI2.txt"
MsgBox "Complete!", vbOKOnly, WScript.ScriptName

EDIT1: 尝试使用vbs转换为iso-8859-1而不是cp1251

Option Explicit

Private Const adReadAll = -1
Private Const adSaveCreateOverWrite = 2
Private Const adTypeBinary = 1
Private Const adTypeText = 2
Private Const adWriteChar = 0

Private Sub UTF8toANSI(ByVal UTF8FName, ByVal ANSIFName)
  Dim strText

  With CreateObject("ADODB.Stream")
    .Open
    .Type = adTypeBinary
    .LoadFromFile UTF8FName
    .Type = adTypeText
    .Charset = "utf-8"
    strText = .ReadText(adReadAll)
    .Position = 0
    .SetEOS
    .Charset = "iso-8859-1"
    .WriteText strText, adWriteChar
    .SaveToFile ANSIFName, adSaveCreateOverWrite
    .Close
  End With
End Sub

UTF8toANSI WScript.Arguments(0), WScript.Arguments(1)

然而这也行不通。

编辑2: 我找到了一种使用stringconverter.exe将文件从UTF转换为ANSI的方法 (从http://www.computerperformance.co.uk/ezine/tools.htm下载)

Setlocal
Set _source=C:\Users\lloyd.EVD\delFirstBat\import
Set _dest=C:\Users\lloyd.EVD\delFirstBat\export
For /F "Tokens=*" %%I In ('dir /b /a-d "%_source%\*.CSV"') Do stringconverter "%_source%\%%~nxI" "%_dest%\%%~nxI" /ANSI

现在,当我删除文件的第一行(无论是之前还是之后,无关紧要)时,它再次成为没有BOM的UTF-8。

所以我现在需要的是一个脚本来del del first row而不更改charset。

1 个答案:

答案 0 :(得分:1)

下一步 VBScript 可以提供帮助:过程UTF8toANSIUTF-8编码的文本文件转换为其他编码。

Option Explicit

Private Const adReadAll = -1
Private Const adSaveCreateOverWrite = 2
Private Const adTypeBinary = 1
Private Const adTypeText = 2
Private Const adWriteChar = 0

Private Sub UTF8toANSI(ByVal UTF8FName, ByVal ANSIFName, ByVal ANSICharSet)
  Dim strText

  With CreateObject("ADODB.Stream")
    .Type = adTypeText

    .Charset = "utf-8"
    .Open
    .LoadFromFile UTF8FName
    strText = .ReadText(adReadAll)
    .Close

    .Charset = ANSICharSet
    .Open
    .WriteText strText, adWriteChar
    .SaveToFile ANSIFName, adSaveCreateOverWrite
    .Close
  End With
End Sub

'UTF8toANSI WScript.Arguments(0), WScript.Arguments(1)
UTF8toANSI "D:\test\SO\38835837utf8.csv", "D:\test\SO\38835837ansi1250.csv", "windows-1250"
UTF8toANSI "D:\test\SO\38835837utf8.csv", "D:\test\SO\38835837ansi1251.csv", "windows-1251"
UTF8toANSI "D:\test\SO\38835837utf8.csv", "D:\test\SO\38835837ansi1253.csv", "windows-1253"

有关系统已知的字符集名称列表,请参阅Windows注册表中HKEY_CLASSES_ROOT\MIME\Database\Charset的子项:

for /F "tokens=5* delims=\" %# in ('reg query HKCR\MIME\Database\Charset') do @echo "%#"

数据38835837utf8.csv档案):

1st Line    1250    852 čeština (Čechie)
2nd Line    1251    966 русский (Россия)
3rd Line    1253    737 ελληνικά (Ελλάδα)

输出表示,那些无法转换为特定字符集的字符会使用Character Decomposition Mapping转换(č => cš => sČ => C等);如果不适用,则全部转换为?问号(常用替换字符):

==> chcp 1250
Active code page: 1250

==> type D:\test\SO\38835837ansi1250.csv
1st Line        1250    852     čeština (Čechie)
2nd Line        1251    966     ??????? (??????)
3rd Line        1253    737     ???????? (??????)

==> chcp 1251
Active code page: 1251

==> type D:\test\SO\38835837ansi1251.csv
1st Line        1250    852     cestina (Cechie)
2nd Line        1251    966     русский (Россия)
3rd Line        1253    737     ???????? (??????)

==> chcp 1253
Active code page: 1253

==> type D:\test\SO\38835837ansi1253.csv
1st Line        1250    852     cestina (Cechie)
2nd Line        1251    966     ??????? (??????)
3rd Line        1253    737     ελληνικά (Ελλάδα)