网站首页  汉语字词  英语词汇  考试资料  写作素材  旧版资料

请输入您要查询的考试资料:

 

标题 vbs实现unicode和ascii编码转换
内容
    一、Copy a Unicode File to an ANSI File
    WiToAnsi.vbs文件:
    ' Utility to rewrite a Unicode text file as an ANSI text file
    ' For use with Windows Scripting Host, CScript.exe or WScript.exe
    ' Copyright (c) 1999, Microsoft Corporation
    '
    Option Explicit
    ' FileSystemObject.CreateTextFile and FileSystemObject.OpenTextFile
    Const OpenAsASCII = 0
    Const OpenAsUnicode = -1
    ' FileSystemObject.CreateTextFile
    Const OverwriteIfExist = -1
    Const FailIfExist = 0
    ' FileSystemObject.OpenTextFile
    Const OpenAsDefault = -2
    Const CreateIfNotExist = -1
    Const FailIfNotExist = 0
    Const ForReading = 1
    Const ForWriting = 2
    Const ForAppending = 8
    Dim argCount:argCount = Wscript.Arguments.Count
    If argCount > 0 Then If InStr(1, Wscript.Arguments(0), "?", vbTextCompare) > 0 Then argCount = 0
    If (argCount = 0) Then
    Wscript.Echo "Utility to copy Unicode text file to an ANSI text file." &_
    vbNewLine & "The 1st argument is the Unicode text file to read" &_
    vbNewLine & "The 2nd argument is the ANSI text file to write" &_
    vbNewLine & "If the 2nd argument is omitted, the Unicode file will be replaced"
    Wscript.Quit 1
    End If
    Dim inFile, outFile, inStream, outStream, inLine, FileSys, WshShell
    If argCount > 1 Then
    outFile = Wscript.Arguments(1)
    inFile = Wscript.Arguments(0)
    Else
    outFile = Wscript.Arguments(0)
    inFile = outFile & ".tmp"
    Set WshShell = Wscript.CreateObject("Wscript.Shell")
    WshShell.Run "cmd.exe /c copy " & outFile & " " & inFile, 0, True
    End If
    Set FileSys = CreateObject("Scripting.FileSystemObject")
    Set inStream = FileSys.OpenTextFile(inFile, ForReading, FailIfNotExist, OpenAsDefault)
    Set outStream = FileSys.CreateTextFile(outFile, OverwriteIfExist, OpenAsASCII)
    Do
    inLine = inStream.ReadLine
    outStream.WriteLine inLine
    Loop Until inStream.AtEndOfStream
    inStream.Close
    outStream.Close
    If argCount = 1 Then WshShell.Run "cmd.exe /c del " & inFile, 0
    批处理中调用:
    cscript WiToAnsi.vbs [path to Unicode file][path to ANSI file]
    二、Copy a ANSI File to an Unicode File
    只需对OpenTextFile和CreateTextFile的打开方式做调整即可。
    三、参考
    四、OpenTextFile和CreateTextFile的使用
    CreateTextFile 方法
    创建指定文件并返回 TextStream 对象,该对象可用于读或写创建的文件。
    object.CreateTextFile(filename[, overwrite[, unicode]])
    参数
    object
    必选项。应为 FileSystemObject 或 Folder 对象的名称。
    filename
    必选项。字符串表达式,指明要创建的文件。
    overwrite
    可选项。Boolean 值指明是否可以覆盖现有文件。如果可覆盖文件,该值为 True;如果不能覆盖文件,则该值为 False 。如果省略该值,则不能覆盖现有文件。
    unicode
    可选项。Boolean 值指明是否以 Unicode 或 ASCII 文件格式创建文件。如果以 Unicode 文件格式创建文件,则该值为 True;如果以 ASCII 文件格式创建文件,则该值为 False。如果省略此部分,则假定创建 ASCII 文件。
    OpenTextFile 方法
    打开指定的文件并返回一个 TextStream 对象,可以读取、写入此对象或将其追加到文件。
    object.OpenTextFile(filename[, iomode[, create[, format]]])
    参数
    object
    必选项。应为 FileSystemObject 对象的名称。
    filename
    必选项。字符串表达式,指明要打开的文件名称。
    iomode
    可选项。输入/输出模式,是下列三个常数之一:ForReading,ForWriting,或 ForAppending。
    create
    可选项。Boolean 值,指出当指定的 filename 不存在时是否能够创建新文件。允许创建新文件时为 True,否则为 False。默认值为 False。
    format
    可选项。三个 Tristate 值之一,指出以何种格式打开文件。若忽略此参数,则文件以 ASCII 格式打开。
    设置
    iomode 参数可为下列设置之一:
    数 值 描述
    ForReading 1 以只读模式打开文件。不能对此文件进行写操作。
    ForWriting 2 以只写方式打开文件。不能对此文件进行读操作。
    ForAppending 8 打开文件并在文件末尾进行写操作。
    format 参数可为下列设置之一:
    常数 值 描述
    TristateUseDefault -2 以系统默认格式打开文件。
    TristateTrue -1 以 Unicode 格式打开文件。
    TristateFalse 0 以 ASCII 格式打开文件。
随便看

 

在线学习网考试资料包含高考、自考、专升本考试、人事考试、公务员考试、大学生村官考试、特岗教师招聘考试、事业单位招聘考试、企业人才招聘、银行招聘、教师招聘、农村信用社招聘、各类资格证书考试等各类考试资料。

 

Copyright © 2002-2024 cuapp.net All Rights Reserved
更新时间:2025/5/18 15:50:48