w10系统txt默认文件编码utf-8怎样更改变为ansi?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了w10系统txt默认文件编码utf-8怎样更改变为ansi?相关的知识,希望对你有一定的参考价值。

另存为的办法就不要说了,要的是新建文档打开就是ansi的格式

如果想修改txt默认编码就把(dword:00000001)的最后一位数字改成下面对应的序号,

然后把下面加粗的内容另存为reg文件,然后双击导入即可。



Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\\Software\\Microsoft\\Notepad]

"iDefaultEncoding"=dword:00000001

; 1 = ANSI

; 2 = UTF-16 LE

; 3 = UTF-16 BE

; 4 = UTF-8 BOM

; 5 = UTF-8


参考技术A 网上各种方法都试验了,没有用。最后选择了安装notepad++, 将打开文本文件默认方式选为notepad++,在noepad++设置中,将新建文件编码设为ANSI格式,OK了。 参考技术B 格式-字体-脚本-选西欧语言,保存

批量将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

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

 



以上是关于w10系统txt默认文件编码utf-8怎样更改变为ansi?的主要内容,如果未能解决你的问题,请参考以下文章

更改文本文档默认编码UTF-8转换ANSI?

文件的编码是一个怎样的机制

如何批量的把WORD文件变为TXT文件???

如何更改win10系统为utf-8

windows怎么更改系统默认编码为utf-8?(避免编译从linux系统弄过来的c文件printf出现乱码问题)(beta版:使用unicode utf-8提供全球语言支持)

记事本默认编码改为UTF-8