标题: [文本处理] [已解决]请问UTF-8如何批量转为ANSI? [打印本页]
作者: 每天几分 时间: 2012-12-1 14:18 标题: [已解决]请问UTF-8如何批量转为ANSI?
本帖最后由 每天几分 于 2012-12-2 17:31 编辑
有些txt文件混杂了UTF-8格式的,数千个文件,不知是哪一个,请问如何将同一文件夹内的UTF-8格式的文本全部转为ANSI格式?
作者: Batcher 时间: 2012-12-1 20:39
http://www.bathome.net/thread-10861-1-1.html
http://www.bathome.net/thread-19842-1-1.html
作者: 每天几分 时间: 2012-12-1 20:55
回复 2# Batcher
不太懂……
作者: Batcher 时间: 2012-12-1 20:56
回复 3# 每天几分
多看几遍
作者: 每天几分 时间: 2012-12-1 21:26
回复 4# Batcher
好吧……
作者: 每天几分 时间: 2012-12-1 22:07
回复 4# Batcher
下面这个VBS可以逐个拖进转换,可以修改一下么,改成把同一文件夹里的UTF格式文件批量转为ANSI。- ' *============================================================================*
- ' * 批量编码转换包括GB2312,UTF-8,Unicode,支持拖动,可扩展到右菜单(发送到) *
- ' * CodeChange.vbs BY: yongfa365 http://www.yongfa365.com 2007-10-04 *
- ' * UTF82ANSI.vbs BY: fastslz http://bbs.cn-dos.net 2007-12-03 *
- ' *============================================================================*
- aCode = "UTF-8"
- bCode = "GB2312"
- Set objArgs = WScript.Arguments
- If objArgs.Count=0 Then
- MsgBox "本脚本仅支持"&aCode&"到"&bCode&"的转换,请将您要转换的文件拖到这个文件上! "&vbCrLf&"命令行用法:UTF82ANSI.vbs [驱动器][目录][文件名1][文件名2]...", vbInformation, "拖拽批量编码转换工具 BY: fastslz"
- End If
- For I = 0 To objArgs.Count - 1
- FileUrl = objArgs(I)
- Call CheckCode (FileUrl)
- Call WriteToFile(FileUrl, ReadFile(FileUrl, aCode), bCode)
- Next
- Function ReadFile(FileUrl, CharSet)
- Dim Str
- Set stm = CreateObject("Adodb.Stream")
- stm.Type = 2
- stm.mode = 3
- stm.charset = CharSet
- stm.Open
- stm.loadfromfile FileUrl
- Str = stm.readtext
- stm.Close
- Set stm = Nothing
- ReadFile = Str
- End Function
-
- Function WriteToFile (FileUrl, Str, CharSet)
- Set stm = CreateObject("Adodb.Stream")
- stm.Type = 2
- stm.mode = 3
- stm.charset = CharSet
- stm.Open
- stm.WriteText Str
- stm.SaveToFile FileUrl, 2
- stm.flush
- stm.Close
- Set stm = Nothing
- End Function
-
- Function CheckCode (FileUrl)
- Dim slz
- set slz = CreateObject("Adodb.Stream")
- slz.Type = 1
- slz.Mode = 3
- slz.Open
- slz.Position = 0
- slz.Loadfromfile FileUrl
- Bin=slz.read(2)
- if AscB(MidB(Bin,1,1))=&HEF and AscB(MidB(Bin,2,1))=&HBB 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
- if not aCode = Codes Then
- MsgBox "待处理文件 “"&FileUrl&"”"&vbCrLf&"该文件原始编码不是"&aCode&",本脚本仅支持"&aCode&"到"&bCode&"的转换! ",vbInformation,"错误终止"
- WScript.Quit
- end if
- slz.Close
- set slz = Nothing
- End Function
复制代码
作者: Batcher 时间: 2012-12-1 22:28
回复 6# 每天几分
到VBS版块发个帖子问问吧
作者: 每天几分 时间: 2012-12-1 22:35
回复 7# Batcher
好吧,再等等,没人回复的话我再到VBS板块问问……
作者: apang 时间: 2012-12-2 16:00
- aCode = "UTF-8"
- bCode = "GB2312"
- Set FSO = CreateObject("Scripting.FileSystemObject")
- For Each FileUrl In FSO.GetFolder(".").Files
- Ext = FSO.GetExtensionName(FileUrl)
- If Lcase(Ext) = "txt" Then
- If CheckCode(FileUrl) Or is_valid_utf8(read(FileUrl)) Then
- Call WriteToFile(FileUrl, ReadFile(FileUrl, aCode), bCode)
- End If
- End If
- Next
- MsgBox "OK"
-
- Function ReadFile(FileUrl, CharSet)
- Dim Str
- Set stm = CreateObject("Adodb.Stream")
- stm.Type = 2
- stm.mode = 3
- stm.charset = CharSet
- stm.Open
- stm.loadfromfile FileUrl
- Str = stm.readtext
- stm.Close
- Set stm = Nothing
- ReadFile = Str
- End Function
-
- Function WriteToFile (FileUrl, Str, CharSet)
- Set stm = CreateObject("Adodb.Stream")
- stm.Type = 2
- stm.mode = 3
- stm.charset = CharSet
- stm.Open
- stm.WriteText Str
- stm.SaveToFile FileUrl, 2
- stm.flush
- stm.Close
- Set stm = Nothing
- End Function
-
- Function CheckCode (FileUrl)
- Dim slz
- Set slz = CreateObject("Adodb.Stream")
- slz.Type = 1
- slz.Mode = 3
- slz.Open
- slz.Position = 0
- slz.Loadfromfile FileUrl
- Bin=slz.read(2)
- If AscB(MidB(Bin,1,1))=&HEF and AscB(MidB(Bin,2,1))=&HBB Then
- CheckCode = True
- end if
- slz.Close
- Set slz = Nothing
- End Function
-
- 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
-
- 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
复制代码
无BOM头的UTF-8文件判断用的是:
http://demon.tw/programming/vbs-validate-utf8.html
作者: ly88888 时间: 2019-12-17 10:37
本帖最后由 ly88888 于 2019-12-17 11:57 编辑
回复 9# apang
完美
欢迎光临 批处理之家 (http://bbs.bathome.net/) |
Powered by Discuz! 7.2 |