返回顶部

收藏

批量编码转换包括gb2312,utf-8,unicode,支持拖动

更多

批量编码转换包括gb2312,utf-8,unicode,支持拖动

[ASP/Basic]代码

    '/*=========================================================================  
    ' * Intro       把要转换的文件拖到这个文件上,选择源编码与目标编码进行编码批量转换  
    ' * FileName    CodeChange.vbs  
    ' * Author      yongfa365  
    ' * Version     v1.0  
    ' * WEB         http://www.yongfa365.com  
    ' * Email       yongfa365[at]qq.com  
    ' * FirstWrite  http://www.yongfa365.com/Item/CodeChange.vbs.html  
    ' * LastModify  2007-10-04 14:29:44  
    ' *==========================================================================*/  

    Set objArgs = WScript.Arguments  
    If objArgs.Count>0 Then  
        CodeI = InputBox("您所拖动的文件的编码是:", "第一步,共两步", "gb2312")  
        CodeII = InputBox("您要将第一步的编码转换为:", "第二步,共两步", "utf-8")  
        If CodeI<>"" And CodeII<>"" Then  
            For I = 0 To objArgs.Count - 1  
                FileUrl = objArgs(I)  
                Call WriteToFile(FileUrl, ReadFile(FileUrl, CodeI), CodeII)  
            Next  
        Else  
            MsgBox "输入不合法,退出"  
        End If  
    Else  
        MsgBox "请将您要转换的“相同编码的文件”拖到这个文件上"&vbCrLf&"程序会根据您的输入转换这些编码", vbInformation, "柳永法温馨提示:"  
    End If  

    '-------------------------------------------------  
    '函数名称:ReadFile  
    '作用:利用AdoDb.Stream对象来读取各种格式的文本文件  
    '----------------------------------------------------  

    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  

    '-------------------------------------------------  
    '函数名称:WriteToFile  
    '作用:利用AdoDb.Stream对象来写入各种格式的文本文件  
    '参数:FileUrl-文件相对路径;Str-文件内容;CharSet-编码格式(utf-8,gb2312.....)  
    '----------------------------------------------------  

    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  

[ASP/Basic]代码

' *==============================================================================*
' * CMD 命令行编码转换工具包括GB2312,UTF-8,Unicode,BIG5...支持拖拽、文件另保存为 *
' * CodeChange.vbs     BY: yongfa365 
' * GB2Ue.vbs          BY: fastslz
' *==============================================================================*
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

标签:ASP.NET/Basic

收藏

0人收藏

支持

0

反对

0

发表评论