VBS implementation GB2312, UTF-8, Unicode, Big5 code conversion tool

Time:2020-1-25

Demonstration
Echo “ABCDE &! @ # $^ < > ()%% ABCDE test! “> before processing.txt“
Gb2ue.vbs “before processing. TXT” “after processing. TXT”
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 times of processing “after processing. TXT” and “before processing. TXT” are still the same code
If you do not know the original encoding of the file, use the

Here are the specific codes:

Copy codeThe code is as follows:
‘ *==============================================================================*
‘* CMD command line encoding conversion tools include GB2312, UTF-8, Unicode, Big5… Support dragging and saving files as*
‘ * CodeChange.vbs BY: yongfa365
‘ * GB2Ue.vbs BY: fastslz
‘ *==============================================================================*
aCode = “GB2312”
bCode = “Unicode”
Show = “this script only supports the conversion from” & acode & “to” & BCODE & “. Please drag a single file to be converted to this file! “
Usage1 = “syntax 1: gb2ue.vbs [drive] [directory] [filename] (directly replace the original file mode)”
Usage2 = “syntax 2: gb2ue.vbs [drive] [directory] [filename] [target drive] [directory] [new name] / Y”
Usage3 = “if the target new file already exists, it will be replaced directly after using the / Y parameter without prompting whether to overwrite it or not! “
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, please use ansi2nicode.vbs”, vbinformation, “program terminated unexpectedly” for dragging and batch processing “
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 (“pending file” “+ Sourcefile +” “= = > target file” “+ GetFile +” “” & vbcrlf & “target file already exists. Do you want to overwrite the existing file? “+ objargs (1) +”, vbquestion + vbyesno, “overwrite or not”)
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 “pending file” “& Sourcefile &” “” & vbcrlf & “the original code of this file is not” & acode & “, this script only supports” & acode & “to” & BCODE & “! “, vbinformation,” error terminated“
WScript.Quit
end if
slz.Close
set slz = Nothing
End Function

Package download address