如何将txt文本文件的ansi编码转换成utf-8编码

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何将txt文本文件的ansi编码转换成utf-8编码相关的知识,希望对你有一定的参考价值。

参考技术A 在桌面新建一个文本文档,不要写入任何问题,然后手动另存为,将此文档编码改为UTF-8,然后将文件名字改为template.txt;//文件名可以随意

将此文件移动到C:\Windows\ShellNew这个文件夹下;

WIN+R输入regedit,打开HKEY_CLASSES_ROOT\.txt\ShellNew,

4
然后新建字符串值FileName,双击这个名字,输入C:\Windows\ShellNew\template.txt本回答被提问者采纳

批量将ANSI文本txt文件转换成UTF8编码格式 (vbs方法)

准备两个文件即可

conv.vbs

run.bat

 

conv.vbs源码

‘用法:将要更改编码的所有文件放到同一个文件夹中,将文件夹拖到该vbs上,输入要转换成的字符编码
Dim fso,fd,fl,f,fdpath,charset
On Error Resume Next
If WScript.Arguments.Length>=1 Then
 fdpath = WScript.Arguments(0)
Else
 fdpath = InputBox("E:xunlian新增加的训练集","第一个参数")
 If fdpath = "" Then WScript.Quit
End If
If WScript.Arguments.Length>=2 Then
 charset = WScript.Arguments(1)
Else
 charset = InputBox("UTF-8","第二个参数")
 if charset = "" then WScript.Quit
 if UCase(charset) = "ANSI" then charset = "GB2312"
End If
Set fso = CreateObject("scripting.filesystemobject")
Set fd = fso.GetFolder(fdpath)
Set fl=fd.Files
For each f in fl
 convertct f.Path,charset
Next
MsgBox "字符编码转换结束",,"tya提示"
  
‘将读取的文件内容以指定编码写入文件
Function convertct(filepath,charset)
 Dim FileName, FileContents, dFileContents
 FileName = filepath 
 FileContents = LoadFile(FileName)
 Set savefile = CreateObject("adodb.stream")
 savefile.Type = 2  ‘这里1为二进制,2为文本型
 savefile.Mode = 3
 savefile.Open()
 savefile.charset = charset
 savefile.Position = savefile.Size
 savefile.Writetext(FileContents)  ‘write写二进制,writetext写文本型
 savefile.SaveToFile filepath,2
 savefile.Close()
 set savefile = nothing
End Function
‘以文件本身编码读取文件
Function LoadFile(Path)
    Dim Stm2
    Set Stm2 = CreateObject("ADODB.Stream")
    Stm2.Type = 2
    Stm2.Mode = 3
    Stm2.Open
    Stm2.Charset = CheckCode(path)
    ‘Stm2.Charset = "UTF-8"
    ‘Stm2.Charset = "Unicode"
    ‘Stm2.Charset = "GB2312"
    Stm2.position = Stm2.Size
    Stm2.LoadFromFile Path
    LoadFile = Stm2.ReadText
    Stm2.Close
    Set Stm2 = Nothing
End Function
‘该函数检查并返回文件的编码类型
Function CheckCode(file)
Dim slz
set slz = CreateObject("Adodb.Stream") 
slz.Type = 1
slz.Mode = 3
slz.Open
slz.Position = 0
slz.Loadfromfile file
Bin=slz.read(2)
If is_valid_utf8(read(file)) Then
Codes="UTF-8"
ElseIf AscB(MidB(Bin,1,1))=&HFF and AscB(MidB(Bin,2,1))=&HFE Then
Codes="Unicode"
Else
Codes="GB2312"
End if
slz.Close
Set slz = Nothing
CheckCode = Codes
End Function
‘将Byte()数组转成String字符串
Function read(path)
    Dim ado, a(), i, n
    Set ado = CreateObject("ADODB.Stream")
    ado.Type = 1 : ado.Open
    ado.LoadFromFile path
    n = ado.Size - 1
    ReDim a(n)
    For i = 0 To n
        a(i) = ChrW(AscB(ado.Read(1)))
    Next
    read = Join(a, "")
End Function
‘准确验证文件是否为utf-8(能验证无BOM头的uft-8文件)
Function is_valid_utf8(ByRef input) ‘ByRef以提高效率
    Dim s, re
    Set re = New Regexp
    s = "[xC0-xDF]([^x80-xBF]|$)"
    s = s & "|[xE0-xEF].{0,1}([^x80-xBF]|$)"
    s = s & "|[xF0-xF7].{0,2}([^x80-xBF]|$)"
    s = s & "|[xF8-xFB].{0,3}([^x80-xBF]|$)"
    s = s & "|[xFC-xFD].{0,4}([^x80-xBF]|$)"
    s = s & "|[xFE-xFE].{0,5}([^x80-xBF]|$)"
    s = s & "|[x00-x7F][x80-xBF]"
    s = s & "|[xC0-xDF].[x80-xBF]"
    s = s & "|[xE0-xEF]..[x80-xBF]"
    s = s & "|[xF0-xF7]...[x80-xBF]"
    s = s & "|[xF8-xFB]....[x80-xBF]"
    s = s & "|[xFC-xFD].....[x80-xBF]"
    s = s & "|[xFE-xFE]......[x80-xBF]"
    s = s & "|^[x80-xBF]"
    re.Pattern = s
    is_valid_utf8 = (Not re.Test(input))
End Function

 

run.bat 源碼

rem  conv.vbs  "E:xunlian新增加的训练集博彩" "UTF-8"
conv.vbs "E:xunlian新增加的训练集博彩" "UTF-8"

  

bat和vbs放到一起,bat内容如下:
rem conv.vbs "改为txt所在文件夹a的路径" "改为要转换成的字符编码"
conv.vbs "改为txt所在文件夹a的路径" "UTF-8"

使用实例见run.bat文件内容,注意待转换的txt文件路径,双击该文件即将执行批处理

歡迎下載:链接:https://pan.baidu.com/s/1uWySdPa-73ByFoM-CACAdA 密码:cul0

心得:以上是看到熱心網友分享的,我也用過,感覺還是蠻好用的,支持。

 



以上是关于如何将txt文本文件的ansi编码转换成utf-8编码的主要内容,如果未能解决你的问题,请参考以下文章

高分求-VB 把ANSI文本转换成UTF-8,多谢!!

批量将ANSI文本txt文件转换成UTF8编码格式 (vbs方法)

如何检查TXT文本是否ANSI编码格式的?

请问用批处理命令如何将ANSI编码的txt文件批量转换utf8编码的?就右键另存为那种,但是量太大。

如何将csv文件转换成Ansi编码的文件?

txt文件,如何人批量把ansi编码变成utf-8编码