banner
李大仁博客

李大仁博客

天地虽大,但有一念向善,心存良知,虽凡夫俗子,皆可为圣贤。

[VBS]文件批量转编码

In my work, I need to convert a large number of files encoded in GB2312 to UTF-8 encoding. I found a very useful VBS script on Baidu that can effectively solve this problem. The usage is also very simple, just add it to the project and call the ConvertFile function. Please note that the UTF-8 files generated using the Adodb.Stream method will have a 3-byte BOM at the beginning, so you need to pay attention when dealing with files that do not require a BOM, such as PHP.

Function ConvertFile(FileUrl)
SrcCode = "gb2312" 'Source encoding
DestCode = "utf-8" 'Target encoding
Call WriteToFile(FileUrl, ReadFile(FileUrl, SrcCode), DestCode)
End Function

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

Reference: http://www.cnblogs.com/fanzhidongyzby/p/3782143.html http://blog.csdn.net/caikanxp/article/details/5614901

加载中...
此文章数据所有权由区块链加密技术和智能合约保障仅归创作者所有。