一、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.OpenTextFileConst OpenAsASCII = 0Const OpenAsUnicode = -1
' FileSystemObject.CreateTextFileConst OverwriteIfExist = -1Const FailIfExist = 0
' FileSystemObject.OpenTextFileConst OpenAsDefault = -2Const CreateIfNotExist = -1Const FailIfNotExist = 0Const ForReading = 1Const ForWriting = 2Const ForAppending = 8
Dim argCount:argCount = Wscript.Arguments.CountIf argCount > 0 Then If InStr(1, Wscript.Arguments(0), "?", vbTextCompare) > 0 Then argCount = 0If (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 1End If
Dim inFile, outFile, inStream, outStream, inLine, FileSys, WshShellIf 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, TrueEnd 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 inLineLoop Until inStream.AtEndOfStreaminStream.CloseoutStream.CloseIf 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的打开方式做调整即可。
三、参考
http://msdn.microsoft.com/en-us/library/aa368046%28VS.85%29.aspx
四、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 参数可为下列设置之一:
format 参数可为下列设置之一:
您可能感兴趣的文章:
- VBS字符串编码转换函数代码
- VBS实现GB2312,UTF-8,Unicode,BIG5编码转换工具