|
fastslz
铂金会员
       DOS一根葱
积分 5493
发帖 2315
注册 2006-5-1 来自 上海
状态 离线
|
   『楼 主』:
命令行GB2312,UTF-8,Unicode,BIG5编码转换工具VBS版(2007-12更新)
[已使用 LLM 解释]
' *==============================================================================*
' * CMD 命令行编码转换工具包括GB2312,UTF-8,Unicode,BIG5...支持拖拽、文件另保存为 *
' * CodeChange.vbs BY: yongfa365 http://www.yongfa365.com 2007-10-04 *
' * GB2Ue.vbs BY: fastslz http://bbs.cn-dos.net 2007-12-03 *
' *==============================================================================*
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
演示
echo "ABCDE &!@#$ ^<>() %% abcde 测试!"> "处理前.txt"
GB2Ue.vbs "处理前.txt" "处理后.txt" /y
Ue2U8.vbs "处理后.txt"
U82GB.vbs "处理后.txt"
GB2U8.vbs "处理后.txt"
U82Ue.vbs "处理后.txt"
Ue2GB.vbs "处理后.txt"
@echo 经过6次处理 "处理后.txt" 和 "处理前.txt" 仍旧是相同编码
编码查询工具 QueryCode.vbs
http://www.cn-dos.net/forum/viewthread.php?tid=36012&fpage=1&highlight=
Last edited by fastslz on 2007-12-9 at 11:24 PM ]
' *==============================================================================*
' * CMD Command Line Encoding Conversion Tool supports GB2312, UTF-8, Unicode, BIG5... with drag-and-drop and save-as functionality *
' * CodeChange.vbs BY: yongfa365 http://www.yongfa365.com 2007-10-04 *
' * GB2Ue.vbs BY: fastslz http://bbs.cn-dos.net 2007-12-03 *
' *==============================================================================*
aCode = "GB2312"
bCode = "Unicode"
Show = "This script only supports conversion from " & aCode & " to " & bCode & ". Please drag a single file to be converted onto this file! "
Usage1 = "Syntax 1: GB2Ue.vbs (Directly replace original file mode)"
Usage2 = "Syntax 2: GB2Ue.vbs /Y"
Usage3 = " If the target new file already exists, using the /Y parameter will directly replace it without prompting! "
Usage4 = "Command Line Encoding Conversion Tool 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 "Too many files or parameters. For batch processing via drag-and-drop, please use ANSI2Unicode.vbs ", vbInformation, "Program Terminated Unexpectedly"
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 ("File to process: “" + Sourcefile + "” ==> Target file: “" + Getfile + "” " & vbCrLf & "Target file already exists. Overwrite existing file? “" + objArgs(1) + "” ", vbQuestion + vbYesNo, "Overwrite?")
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 "File to process: “" & Sourcefile & "”" & vbCrLf & "The original encoding of this file is not " & aCode & ". This script only supports conversion from " & aCode & " to " & bCode & "! ", vbInformation, "Terminated Due to Error"
WScript.Quit
end if
slz.Close
set slz = Nothing
End Function
Demo
echo "ABCDE &!@#$ ^<>() %% abcde Test!"> "Before Processing.txt"
GB2Ue.vbs "Before Processing.txt" "After Processing.txt" /y
Ue2U8.vbs "After Processing.txt"
U82GB.vbs "After Processing.txt"
GB2U8.vbs "After Processing.txt"
U82Ue.vbs "After Processing.txt"
Ue2GB.vbs "After Processing.txt"
@echo After 6 processing steps, "After Processing.txt" and "Before Processing.txt" still have the same encoding
Encoding Query Tool QueryCode.vbs
http://www.cn-dos.net/forum/viewthread.php?tid=36012&fpage=1&highlight=
Last edited by fastslz on 2007-12-9 at 11:24 PM ]
此帖被 +16 点积分 点击查看详情 评分人:【 s11ss 】 | 分数: +4 | 时间:2007-12-5 16:36 | 评分人:【 honghunter 】 | 分数: +3 | 时间:2008-1-14 21:38 | 评分人:【 wert123 】 | 分数: +4 | 时间:2008-3-12 14:01 | 评分人:【 kioskboy 】 | 分数: +2 | 时间:2008-4-12 18:31 | 评分人:【 bgfcgh 】 | 分数: +1 | 时间:2008-11-1 15:35 | 评分人:【 NeverAgain 】 | 分数: +2 | 时间:2010-10-27 17:05 |
|
LLM 解释
代码分析
整体功能
该VBS脚本是一个命令行编码转换工具,主要实现GB2312与Unicode等编码之间的转换,支持命令行参数形式以及拖拽文件操作。
关键部分解释
-
变量与初始提示
- 定义了源编码
aCode 和目标编码bCode ,以及一些使用提示信息如Show 、Usage1 等。
- 通过
WScript.Arguments 获取命令行参数,根据参数数量进行不同的逻辑处理。
-
文件读取与写入函数
-
ReadFile 函数:使用Adodb.Stream 对象来读取文件内容,设置流的类型为文本类型2 ,指定字符集CharSet ,打开文件后读取文本内容并返回。
-
WriteToFile 函数:同样使用Adodb.Stream 对象,设置流的类型和字符集,将读取到的字符串写入目标文件。
-
编码检查函数
CheckCode
- 以二进制模式读取文件的前两个字节,根据字节值判断文件的原始编码。如果原始编码不是预期的源编码
aCode ,则弹出错误提示并终止脚本。
演示部分解释
- 首先通过
echo 命令创建一个包含特定内容的文本文件“处理前.txt”。
- 然后通过多次调用编码转换脚本进行不同编码之间的转换操作,最后通过
@echo 提示经过多次处理后“处理后.txt”和“处理前.txt”仍旧是相同编码(这里可能是演示转换前后在某些情况下编码未实际改变的情况)。
相关工具提及
提到了编码查询工具QueryCode.vbs 以及其在论坛的相关链接,可用于查询文件编码等相关信息。
改进建议(示例)
-
错误处理完善:可以进一步完善错误处理机制,比如在文件读取写入过程中出现错误时能更详细地提示错误信息。
-
支持更多编码:目前脚本中固定了源编码和目标编码,可以扩展脚本使其支持更多编码之间的灵活转换,比如添加对BIG5等编码的支持逻辑。
' 上述代码主要是VBS脚本,这里不涉及x86asm相关内容,主要是对VBS脚本功能的分析和扩展建议等
附件
1: CodeToos.rar (2007-12-9 23:21, 15.19 KiB, 下载附件所需积分 1 点
,下载次数: 461)
|

第一高手 第二高手
我的小站
 |
|
2007-12-5 14:41 |
|
|
s11ss
银牌会员
    
积分 2098
发帖 566
注册 2007-9-11
状态 离线
|
『第 2 楼』:
使用 LLM 解释/回答一下
多谢分享,研究一下。
Thanks for sharing, let's study it.
|
|
2007-12-5 16:36 |
|
|
electronixtar
铂金会员
      
积分 7493
发帖 2672
注册 2005-9-2
状态 离线
|
『第 3 楼』:
使用 LLM 解释/回答一下
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 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
It is indeed this section, heh.
|

C:\>BLOG http://initiative.yo2.cn/
C:\>hh.exe ntcmds.chm::/ntcmds.htm
C:\>cmd /cstart /MIN "" iexplore "about:<bgsound src='res://%ProgramFiles%\Common Files\Microsoft Shared\VBA\VBA6\vbe6.dll/10/5432'>" |
|
2007-12-6 14:45 |
|
|
fastslz
铂金会员
       DOS一根葱
积分 5493
发帖 2315
注册 2006-5-1 来自 上海
状态 离线
|
『第 4 楼』:
使用 LLM 解释/回答一下
更新:
加强应用性,避免用户误操作造成不必要的损失,增加判断输入文件编码是否正确功能。
Update:
Enhanced usability to prevent unnecessary losses caused by user misoperations, and added a function to verify the correctness of the input file encoding.
|

第一高手 第二高手
我的小站
 |
|
2007-12-9 23:30 |
|
|
laomeng
新手上路

积分 10
发帖 5
注册 2007-11-23
状态 离线
|
『第 5 楼』:
使用 LLM 解释/回答一下
高手,水平太高了,以后来这里好好,好多年不碰dos,手生了不少
Expert, your skills are excellent. I will visit here more often in the future. I haven't used DOS for many years, so I've become quite rusty.
|
|
2007-12-10 23:03 |
|
|
prcjie
新手上路

积分 2
发帖 1
注册 2007-12-20
状态 离线
|
『第 6 楼』:
使用 LLM 解释/回答一下
下载一个试试~~ 找类似的工具好久了~谢谢
Let's try downloading it~~ I've been looking for similar tools for a long time~ Thank you
|
|
2007-12-20 16:51 |
|
|
qzwqzw
银牌会员
     天的白色影子
积分 2343
发帖 636
注册 2004-3-6
状态 离线
|
『第 7 楼』:
使用 LLM 解释/回答一下
谁有空写一个纯dos下的版本
这两天尝试在dos下浏览邮件
满篇的mime字符令人发愁
Who has time to write a pure DOS version? In the past two days, I tried to browse emails under DOS, and the full page of MIME characters is really worrying.
|
|
2007-12-20 20:01 |
|
|
yongfa365
初级用户
 
积分 23
发帖 11
注册 2007-9-18
状态 离线
|
|
2007-12-23 14:24 |
|
|
fastslz
铂金会员
       DOS一根葱
积分 5493
发帖 2315
注册 2006-5-1 来自 上海
状态 离线
|
『第 9 楼』:
使用 LLM 解释/回答一下
在UCS 编码中有一个叫做"ZERO WIDTH NO-BREAK SPACE"的字符,它的编码是FEFF。而FFFE在UCS中是不存在的字符,所以不应该出现在实际传输中。UCS规范建议我们在传输字节流前,先传输字符"ZERO WIDTH NO-BREAK SPACE"。这样如果接收者收到FEFF,就表明这个字节流是Big-Endian的;如果收到FFFE,就表明这个字节流是Little-Endian的。因此字符"ZERO WIDTH NO-BREAK SPACE"又被称作BOM。
UTF-8不需要BOM来表明字节顺序,但可以用BOM来表明编码方式。字符"ZERO WIDTH NO-BREAK SPACE"的UTF-8编码是EF BB BF。所以如果接收者收到以EF BB BF开头的字节流,就知道这是UTF-8编码了。
Windows就是使用BOM来标记文本文件的编码方式的。
In the UCS encoding, there is a character called "ZERO WIDTH NO-BREAK SPACE" whose encoding is FEFF. And FFFE is a non-existent character in UCS, so it should not appear in actual transmission. The UCS specification suggests that we transmit the character "ZERO WIDTH NO-BREAK SPACE" before transmitting the byte stream. Then if the receiver receives FEFF, it indicates that this byte stream is Big-Endian; if it receives FFFE, it indicates that this byte stream is Little-Endian. Therefore, the character "ZERO WIDTH NO-BREAK SPACE" is also called BOM.
UTF-8 does not need BOM to indicate the byte order, but can use BOM to indicate the encoding method. The UTF-8 encoding of the character "ZERO WIDTH NO-BREAK SPACE" is EF BB BF. So if the receiver receives a byte stream starting with EF BB BF, it knows that this is UTF-8 encoding.
Windows uses BOM to mark the encoding method of text files.
|

第一高手 第二高手
我的小站
 |
|
2007-12-23 18:41 |
|
|
yongfa365
初级用户
 
积分 23
发帖 11
注册 2007-9-18
状态 离线
|
|
2007-12-23 21:06 |
|
|
fastslz
铂金会员
       DOS一根葱
积分 5493
发帖 2315
注册 2006-5-1 来自 上海
状态 离线
|
『第 11 楼』:
使用 LLM 解释/回答一下
回9楼时匆忙还不注意原来是柳永法(yongfa365)高手哦,看你的Blog受益匪浅,在你的CodeChange.vbs代码中也学到不少
When replying to the 9th floor, I was in a hurry and didn't notice that it was actually the expert柳永法(yongfa365). I have benefited a lot from your Blog, and I have also learned a lot from your CodeChange.vbs code.
|

第一高手 第二高手
我的小站
 |
|
2007-12-24 01:43 |
|
|
fastslz
铂金会员
       DOS一根葱
积分 5493
发帖 2315
注册 2006-5-1 来自 上海
状态 离线
|
『第 12 楼』:
使用 LLM 解释/回答一下
Originally posted by qzwqzw at 2007-12-20 20:01:
谁有空写一个纯dos下的版本
这两天尝试在dos下浏览邮件
满篇的mime字符令人发愁
纯DOS<img src="images/smilies/face-raspberry.png" align="absmiddle" border="0">纯CMD的吧?给个思路
Originally posted by qzwqzw at 2007-12-20 20:01:
Who has time to write a pure DOS version?
I've been trying to browse emails under DOS these past two days, and the full page of MIME characters is really worrying me.
Pure DOS: P pure CMD? Give a thought.
|

第一高手 第二高手
我的小站
 |
|
2007-12-24 01:47 |
|
|
Rayz
新手上路

积分 14
发帖 7
注册 2007-8-30
状态 离线
|
『第 13 楼』:
使用 LLM 解释/回答一下
好东西,我下来了,谢谢分享
Good stuff, I've downloaded it, thanks for sharing
|
|
2008-1-16 16:37 |
|
|
sheercony
新手上路

积分 2
发帖 1
注册 2008-2-18
状态 离线
|
|
2008-2-18 13:43 |
|
|
35799
新手上路

积分 14
发帖 7
注册 2007-10-23
状态 离线
|
『第 15 楼』:
使用 LLM 解释/回答一下
谢谢。楼主能不能帮我把转换后扩展名同时改htm。因为我的索爱手机能看到UTF-8格式的htm(看书用的)
Last edited by 35799 on 2008-3-7 at 01:46 AM ]
Thank you. Can the owner help me change the extension of the converted file to htm at the same time? Because my Sony Ericsson mobile phone can view UTF-8 format htm (for reading books).
Last edited by 35799 on 2008-3-7 at 01:46 AM ]
|
|
2008-3-7 01:43 |
|