足球游戏_中国足彩网¥体育资讯$

vbs实现unicode和ascii编码转换
来源:易贤网 阅读:1817 次 日期:2014-12-16 15:45:13
温馨提示:易贤网小编为您整理了“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 格式打开文件。

中国足彩网信息请查看IT技术专栏

中国足彩网信息请查看脚本栏目
易贤网手机网站地址:vbs实现unicode和ascii编码转换
由于各方面情况的不断调整与变化,易贤网提供的所有考试信息和咨询回复仅供参考,敬请考生以权威部门公布的正式信息和咨询为准!
关于我们 | 联系我们 | 人才招聘 | 网站声明 | 网站帮助 | 非正式的简要咨询 | 简要咨询须知 | 加入群交流 | 手机站点 | 投诉建议
工业和信息化部备案号:滇ICP备2023014141号-1 足球游戏_中国足彩网¥体育资讯$ 滇公网安备53010202001879号 人力资源服务许可证:(云)人服证字(2023)第0102001523号
云南网警备案专用图标
联系电话:0871-65317125(9:00—18:00) 获取招聘考试信息及咨询关注公众号:hfpxwx
咨询QQ:526150442(9:00—18:00)版权所有:易贤网
云南网警报警专用图标