China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-06-30 15:08
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Command-Line GB2312, UTF-8, Unicode, BIG5 Encoding Conversion Tool VBS Version (Updated December 2007) View 17,722 Replies 64
Original Poster Posted 2007-12-05 14:41 ·  中国 上海 松江区 电信
铂金会员
★★★★
DOS一根葱
Credits 5,493
Posts 2,315
Joined 2006-05-01 10:41
20-year member
UID 54766
Gender Male
From 上海
Status Offline
' *==============================================================================*
' * 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 ]
Recent Ratings for This Post ( 6 in total) Click for details
RaterScoreTime
s11ss +4 2007-12-05 16:36
honghunter +3 2008-01-14 21:38
wert123 +4 2008-03-12 14:01
kioskboy +2 2008-04-12 18:31
bgfcgh +1 2008-11-01 15:35
NeverAgain +2 2010-10-27 17:05
Attachments
CodeToos.rar (15.19 KiB, Credits to download 1 pts, Downloads: 461)
第一高手 第二高手

Floor 2 Posted 2007-12-05 16:36 ·  中国 北京 电信
银牌会员
★★★
Credits 2,098
Posts 566
Joined 2007-09-11 07:27
18-year member
UID 97070
Gender Male
Status Offline
Thanks for sharing, let's study it.
Floor 3 Posted 2007-12-06 14:45 ·  中国 四川 成都 教育网
铂金会员
★★★★
Credits 7,493
Posts 2,672
Joined 2005-09-02 00:00
20-year member
UID 42173
Gender Male
Status Offline
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'>"
Floor 4 Posted 2007-12-09 23:30 ·  中国 上海 松江区 电信
铂金会员
★★★★
DOS一根葱
Credits 5,493
Posts 2,315
Joined 2006-05-01 10:41
20-year member
UID 54766
Gender Male
From 上海
Status Offline
Update:
Enhanced usability to prevent unnecessary losses caused by user misoperations, and added a function to verify the correctness of the input file encoding.
第一高手 第二高手

Floor 5 Posted 2007-12-10 23:03 ·  中国 山东 济南 联通
新手上路
Credits 10
Posts 5
Joined 2007-11-23 18:08
18-year member
UID 103545
Gender Male
Status Offline
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.
Floor 6 Posted 2007-12-20 16:51 ·  中国 北京 联通
新手上路
Credits 2
Posts 1
Joined 2007-12-20 16:25
18-year member
UID 106261
Gender Male
Status Offline
Let's try downloading it~~ I've been looking for similar tools for a long time~ Thank you
Floor 7 Posted 2007-12-20 20:01 ·  中国 山西 运城 联通
银牌会员
★★★
天的白色影子
Credits 2,343
Posts 636
Joined 2004-03-06 00:00
22-year member
UID 19350
Gender Male
Status Offline
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.
Floor 8 Posted 2007-12-23 14:24 ·  中国 北京 朝阳区 联通
初级用户
Credits 23
Posts 11
Joined 2007-09-18 13:38
18-year member
UID 97692
Gender Male
Status Offline
The function CheckCode can only judge files with BOM
Floor 9 Posted 2007-12-23 18:41 ·  中国 上海 松江区 电信
铂金会员
★★★★
DOS一根葱
Credits 5,493
Posts 2,315
Joined 2006-05-01 10:41
20-year member
UID 54766
Gender Male
From 上海
Status Offline
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.
第一高手 第二高手

Floor 10 Posted 2007-12-23 21:06 ·  中国 北京 朝阳区 联通
初级用户
Credits 23
Posts 11
Joined 2007-09-18 13:38
18-year member
UID 97692
Gender Male
Status Offline
There are many problems with BOM in many places. I don't like this BOM.
Floor 11 Posted 2007-12-24 01:43 ·  中国 上海 松江区 电信
铂金会员
★★★★
DOS一根葱
Credits 5,493
Posts 2,315
Joined 2006-05-01 10:41
20-year member
UID 54766
Gender Male
From 上海
Status Offline
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.
第一高手 第二高手

Floor 12 Posted 2007-12-24 01:47 ·  中国 上海 松江区 电信
铂金会员
★★★★
DOS一根葱
Credits 5,493
Posts 2,315
Joined 2006-05-01 10:41
20-year member
UID 54766
Gender Male
From 上海
Status Offline
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.
第一高手 第二高手

Floor 13 Posted 2008-01-16 16:37 ·  中国 浙江 电信
新手上路
Credits 14
Posts 7
Joined 2007-08-30 12:16
18-year member
UID 96172
Gender Male
Status Offline
Good stuff, I've downloaded it, thanks for sharing
Floor 14 Posted 2008-02-18 13:43 ·  中国 山东 青岛 联通
新手上路
Credits 2
Posts 1
Joined 2008-02-18 13:20
18-year member
UID 110888
Gender Male
Status Offline
Great! This is really a good thing
Floor 15 Posted 2008-03-07 01:43 ·  中国 广东 佛山 三水区 电信
新手上路
Credits 14
Posts 7
Joined 2007-10-23 08:50
18-year member
UID 100506
Gender Male
Status Offline
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 ]
1 2 3 5 Next ›
Forum Jump: