一个转换编码格式的VBS脚本

今天看到了一个转换编码格式的VBS脚本程序,但是只能转换2种,改造了一下,可以自己指定转换格式。支持:

  • utf-8
  • ansi 或者是 gb2312
  • unicode

之间的相互转换。自动探测文件格式,手工指定输出格式,默认输出格式为utf-8。可以直接改变文件编码,也可以输出为另外的一个文件。支持强制覆盖选项。

用法为:

cscript chcode.vbs inputfile.txt

会把inputfile.txt转换为utf-8格式编码

cscript chcode.vbs inputfile.txt outputfile.txt -o ansi

会把inputfile.txt转成为ansi编码的outputfil.txt,原文件不变。

代码如下:

' Change the code of a file.
' By Eric Brown
' 2015/3/20


'Get the arguments
Set argv = WScript.Arguments

if argv.Length = 0 Then
    Call PrintHelp
end if

'The default code
inCode = ""
outCode = "utf-8"

haveOut = False
haveInCode = False
getInput = False
inFile = ""
outFile = ""
'If force, will replace the output
'without asking.
force = False

'Analyse the arguments
For i = 0 to argv.Length - 1
    isOption = False
    if StrComp(argv(i), "-i", vbTextCompare) = 0 Then
        i = i + 1
        inCode = argv(i)
        haveInCode = True
        isOption = True
    elseif StrComp(argv(i), "-o", vbTextCompare) = 0 Then
        i = i + 1
        outCode = argv(i)
        isOption = True
    elseif StrComp(argv(i), "-f", vbTextCompare) = 0 Then
        force = True
        isOption = True
    elseif StrComp(argv(i), "-h", vbTextCompare) = 0 Then
        Call PrintHelp
    end if

    if not isOption then
        if not getInput Then
            inFile = argv(i)
            getInput = True
        else
            outFile = argv(i)
            haveOut = True
        end if
    end if
Next

if not haveOut then
    outFile = inFile
end if

'If output isn't specified
'Make a temp file
if not haveOut then
    outFile = outFile & "~~~~~~~"
end if
'Is the output format supported?
if StrComp(LCase(outCode), "utf-8") <> 0 and _
   StrComp(LCase(outCode), "gb2312") <> 0 and _
   StrComp(LCase(outCode), "unicode") <> 0 then
    if StrComp(LCase(outCode), "ansi") then
        outCode = "gb2312"
    else
        WScript.echo "Unsupported format: " & outCode
        WScript.Quit
    end if
end if
if StrComp(LCase(inCode), "ansi") = 0 then
    inCode = "gb2312"
end if

Call CheckCode(inFile)

'If the output is already existed
'Check it unless -f is specified
Set fso = CreateObject("Scripting.FileSystemObject")
if fso.FileExists(outFile) and not force then
    choice = Msgbox(outFile & " has been existed!" & vbCrlf & _
            " Do you want to replace it?", vbQuestion + vbYesNo, _
            "Output file has been existed")
    if choice = vbNo Then
        WScript.Quit
    end if
end if

Set instream = CreateObject("Adodb.Stream")
Set outstream = CreateObject("Adodb.Stream")

'Open input file
instream.Type = 2 'adTypeText
instream.Mode = 3 'adModeReadWrite
instream.Charset = inCode
instream.Open
instream.LoadFromFile inFile

'Read input file
content = instream.ReadText

'Close input file
instream.Close
Set instream = Nothing

'Open output file
outstream.Type = 2 'adTypeText
outstream.Mode = 3 'adModeReadWrite
outstream.Charset = outCode
outstream.Open

'Write to output file
outstream.WriteText content
outstream.SaveToFile outFile, 2 'adSaveCreateOverWrite
outstream.flush

'Close output file
outstream.Close
Set outstream = Nothing

'If not specify the output file
'then replace the input file
if not haveOut then
    set srcFile = fso.getFile(inFile)
    srcFile.delete
    set srcFile = fso.getFile(outFile)
    srcFile.name = inFile
end if


Function CheckCode(Sourcefile)
    'WScript.echo "Checking: " & Sourcefile
    Dim stream
    set stream = CreateObject("Adodb.Stream")
    stream.Type = 1 'adTypeBinary
    stream.Mode = 3 'adModeReadWrite
    stream.Open
    stream.Position = 0
    stream.LoadFromFile Sourcefile
    Bin = stream.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 haveInCode Then
        inCode = Codes
    end if
    if StrComp(LCase(inCode), Codes) <> 0 then
        WScript.echo "Detected input format is: " & Codes &_
            vbCrlf & "But you specified " & inCode & "."
        WScript.Quit
    end if
    stream.Close
    set stream = Nothing
end Function

Function PrintHelp()
    message = "Usage: cscript chcode.vbs inFileName (outFileName) " & _
                "(Options)" & vbCrlf & _
                "If the outFileName is not specified, this program " & _
                "will change the inFileName's code" & vbCrlf & _
                "OPTIONS" & vbCrlf & _
                "    -i [inCode]: Specify the code " & _
                "of input file."& vbCrlf &_
                "                 If not specified, program will " & _
                "auto detect the code of the input file." & vbCrlf & _
                "    -o [outCode]: Specify the code of output file." & _
                vbCrlf & "                  The default is utf-8" & _
                vbCrlf & _
                "    -f: If the output file is specified, don't ask." & _
                vbCrlf & "SUPPORTED FORMAT" & vbCrlf & _
                "      utf-8" & vbCrlf & _
                "      ansi or gb2312" & vbCrlf & _
                "      unicode" & vbCrlf
    WScript.echo message
    WScript.Quit
end Function
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值