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的打开方式做调整即可。

三 参考
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 参数可为下列设置之一:

常数描述
ForReading1以只读模式打开文件。不能对此文件进行写操作。
ForWriting2以只写方式打开文件。不能对此文件进行读操作。
ForAppending8打开文件并在文件末尾进行写操作。

 format 参数可为下列设置之一:

常数描述
TristateUseDefault-2以系统默认格式打开文件。
TristateTrue-1以 Unicode 格式打开文件。
TristateFalse 0以 ASCII 格式打开文件。

 

完!

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值