用VBS实现的批量gb2312转utf-8,支持拖动
程序员文章站
2022-07-04 20:33:48
复制代码 代码如下:'/*=========================================================================...
复制代码 代码如下:
'/*=========================================================================
' * intro 拖动所有要转换的gb2312编码文件到这个文件上,程序会将它们自动转换为utf-8编码文件
' * filename gb2312.to.utf-8.vbs
' * author yongfa365
' * version v1.0
' * web http://www.yongfa365.com
' * email yongfa365[at]qq.com
' * lastmodify 2007-10-04 10:42:53
' *==========================================================================*/
set objargs = wscript.arguments
if objargs.count>0 then
if msgbox("本程序仅支持gb2312到utf-8的转换"&vbcrlf&"如果您所拖动的文件里有别的格式的文件请点“取消”", vbokcancel + vbexclamation + vbdefaultbutton2, "郑重提醒!!!") = vbok then
for i = 0 to objargs.count - 1
fileurl = objargs(i)
call writetofile(fileurl, readfile(fileurl, "gb2312"), "utf-8")
next
end if
else
msgbox "请将您要转换的“gb2312文件”拖到这个文件上"&vbcrlf&"程序会将它们自动转换为utf-8文件", vbinformation, "柳永法温馨提示:"
end if
'-------------------------------------------------
'函数名称:readfile
'作用:利用adodb.stream对象来读取各种格式的文本文件
'----------------------------------------------------
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
'-------------------------------------------------
'函数名称:writetofile
'作用:利用adodb.stream对象来写入各种格式的文本文件
'参数:fileurl-文件相对路径;str-文件内容;charset-编码格式(utf-8,gb2312.....)
'----------------------------------------------------
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