欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

VBS实现GB2312,UTF-8,Unicode,BIG5编码转换工具

程序员文章站 2022-07-04 20:47:13
演示 echo "abcde &!@#$ ^<>() %% abcde 测试!"> "处理前.txt" gb2ue.vbs "处理前.txt" "处理后....
演示
echo "abcde &!@#$ ^<>() %% abcde 测试!"> "处理前.txt"
gb2ue.vbs "处理前.txt" "处理后.txt"
ue2u8.vbs "处理后.txt"
u82gb.vbs "处理后.txt"
gb2u8.vbs "处理后.txt"
u82ue.vbs "处理后.txt"
ue2gb.vbs "处理后.txt"
@echo 经过6次处理 "处理后.txt" 和 "处理前.txt" 仍旧是相同编码
如果不知道文件的原始编码,请使用

下面里面里面具体代码:
复制代码 代码如下:

' *==============================================================================*
' * cmd 命令行编码转换工具包括gb2312,utf-8,unicode,big5...支持拖拽、文件另保存为 *
' * codechange.vbs by: yongfa365
' * gb2ue.vbs by: fastslz
' *==============================================================================*
acode = "gb2312"
bcode = "unicode"
show = "本脚本仅支持"&acode&"到"&bcode&"的转换,请拖拽单个要转换的文件到此文件上! "
usage1 = "语法1:gb2ue.vbs [驱动器][目录][文件名] (直接替换原文件模式)"
usage2 = "语法2:gb2ue.vbs [驱动器][目录][文件名] [目标驱动器][目录][新名称] /y"
usage3 = " 如果目标新文件已存在,使用/y参数后将直接替换而不提示是否改写! "
usage4 = "命令行编码转换工具 by: fastslz"

set objargs=wscript.arguments
set fso=createobject("scripting.filesystemobject")
if objargs.count=0 then
msgbox show &vbcrlf&vbcrlf& usage1 &vbcrlf& usage2 &vbcrlf& usage3, vbinformation, usage4
wscript.quit
end if
if not objargs.count < 3 then
options="/y"
ignoring = strcomp(objargs(2), options, vbtextcompare)
if ignoring = 0 then
sourcefile=objargs(0)
getfile=objargs(1)
else
msgbox "文件数量或参数太多,拖拽批量处理请用 ansi2unicode.vbs ", vbinformation, "程序意外终止"
wscript.quit
end if
else
if not objargs.count < 2 then
sourcefile=objargs(0)
getfile=objargs(1)
if fso.fileexists(objargs(1)) then
choice = msgbox ("待处理文件“"+sourcefile+"” ==> 目标文件“"+getfile+"” "&vbcrlf&"目标文件已存在,是否改写现有文件?“"+objargs(1)+"” ",vbquestion+vbyesno,"是否改写")
if choice = vbyes then
getfile=objargs(1)
else
wscript.quit
end if
end if
else
sourcefile=objargs(0)
getfile=objargs(0)
end if
end if

call checkcode (sourcefile)
call writetofile(getfile, readfile(sourcefile, acode), bcode)
wscript.quit

function readfile (sourcefile, charset)
dim str
set stm = createobject("adodb.stream")
stm.type = 2
stm.mode = 3
stm.charset = charset
stm.open
stm.loadfromfile sourcefile
str = stm.readtext
stm.close
set stm = nothing
readfile = str
end function

function writetofile (getfile, str, charset)
set stm = createobject("adodb.stream")
stm.type = 2
stm.mode = 3
stm.charset = charset
stm.open
stm.writetext str
stm.savetofile getfile,2
stm.flush
stm.close
set stm = nothing
end function

function checkcode (sourcefile)
dim slz
set slz = createobject("adodb.stream")
slz.type = 1
slz.mode = 3
slz.open
slz.position = 0
slz.loadfromfile sourcefile
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 "待处理文件 “"&sourcefile&"”"&vbcrlf&"该文件原始编码不是"&acode&",本脚本仅支持"&acode&"到"&bcode&"的转换! ",vbinformation,"错误终止"
wscript.quit
end if
slz.close
set slz = nothing
end function