vbs實(shí)現(xiàn)unicode和ascii編碼轉(zhuǎn)換
一、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
批處理中調(diào)用:
cscript WiToAnsi.vbs [path to Unicode file][path to ANSI file]
二、Copy a ANSI File to an Unicode File
只需對(duì)OpenTextFile和CreateTextFile的打開方式做調(diào)整即可。
三、參考
http://msdn.microsoft.com/en-us/library/aa368046%28VS.85%29.aspx
四、OpenTextFile和CreateTextFile的使用
CreateTextFile 方法
創(chuàng)建指定文件并返回 TextStream 對(duì)象,該對(duì)象可用于讀或?qū)憚?chuàng)建的文件。
object.CreateTextFile(filename[, overwrite[, unicode]])
參數(shù)
object
必選項(xiàng)。應(yīng)為 FileSystemObject 或 Folder 對(duì)象的名稱。
filename
必選項(xiàng)。字符串表達(dá)式,指明要?jiǎng)?chuàng)建的文件。
overwrite
可選項(xiàng)。Boolean 值指明是否可以覆蓋現(xiàn)有文件。如果可覆蓋文件,該值為 True;如果不能覆蓋文件,則該值為 False 。如果省略該值,則不能覆蓋現(xiàn)有文件。
unicode
可選項(xiàng)。Boolean 值指明是否以 Unicode 或 ASCII 文件格式創(chuàng)建文件。如果以 Unicode 文件格式創(chuàng)建文件,則該值為 True;如果以 ASCII 文件格式創(chuàng)建文件,則該值為 False。如果省略此部分,則假定創(chuàng)建 ASCII 文件。
OpenTextFile 方法
打開指定的文件并返回一個(gè) TextStream 對(duì)象,可以讀取、寫入此對(duì)象或?qū)⑵渥芳拥轿募?br />
object.OpenTextFile(filename[, iomode[, create[, format]]])
參數(shù)
object
必選項(xiàng)。應(yīng)為 FileSystemObject 對(duì)象的名稱。
filename
必選項(xiàng)。字符串表達(dá)式,指明要打開的文件名稱。
iomode
可選項(xiàng)。輸入/輸出模式,是下列三個(gè)常數(shù)之一:ForReading,F(xiàn)orWriting,或 ForAppending。
create
可選項(xiàng)。Boolean 值,指出當(dāng)指定的 filename 不存在時(shí)是否能夠創(chuàng)建新文件。允許創(chuàng)建新文件時(shí)為 True,否則為 False。默認(rèn)值為 False。
format
可選項(xiàng)。三個(gè) Tristate 值之一,指出以何種格式打開文件。若忽略此參數(shù),則文件以 ASCII 格式打開。
設(shè)置
iomode 參數(shù)可為下列設(shè)置之一:
數(shù) | 值 | 描述 |
---|---|---|
ForReading | 1 | 以只讀模式打開文件。不能對(duì)此文件進(jìn)行寫操作。 |
ForWriting | 2 | 以只寫方式打開文件。不能對(duì)此文件進(jìn)行讀操作。 |
ForAppending | 8 | 打開文件并在文件末尾進(jìn)行寫操作。 |
format 參數(shù)可為下列設(shè)置之一:
常數(shù) | 值 | 描述 |
---|---|---|
TristateUseDefault | -2 | 以系統(tǒng)默認(rèn)格式打開文件。 |
TristateTrue | -1 | 以 Unicode 格式打開文件。 |
TristateFalse | 0 | 以 ASCII 格式打開文件。 |
上一篇:VBS顯示當(dāng)前標(biāo)準(zhǔn)時(shí)間
欄 目:vb
下一篇:VBS實(shí)現(xiàn)將當(dāng)前時(shí)間轉(zhuǎn)換成UTC時(shí)間
本文標(biāo)題:vbs實(shí)現(xiàn)unicode和ascii編碼轉(zhuǎn)換
本文地址:http://mengdiqiu.com.cn/a1/vb/7196.html
您可能感興趣的文章
- 01-10下載文件到本地運(yùn)行的vbs
- 01-10VBS中的正則表達(dá)式的用法大全 <font color=red>原創(chuàng)&
- 01-10VBS中SendKeys的基本應(yīng)用
- 01-10VBScript教程 第十一課深入VBScript
- 01-10用VBSCRIPT控制ONSUBMIT事件
- 01-10VBScript語(yǔ)法速查及實(shí)例說(shuō)明
- 01-10VBS中Select CASE的其它用法
- 01-10VBScript教程 第七課使用條件語(yǔ)句
- 01-10vbscript 可以按引用傳遞參數(shù)嗎?
- 01-10VBScript教程 第二課在HTML頁(yè)面中添加VBscript代碼


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-10下載文件到本地運(yùn)行的vbs
- 01-10飄葉千夫指源代碼,又稱qq刷屏器
- 01-10SendKeys參考文檔
- 01-10什么是一個(gè)高效的軟件
- 01-10VBS中的正則表達(dá)式的用法大全 &l
- 01-10exe2swf 工具(Adodb.Stream版)
- 01-10VBS中SendKeys的基本應(yīng)用
- 01-10用VBSCRIPT控制ONSUBMIT事件
- 01-10VBScript教程 第十一課深入VBScript
- 01-10VBScript語(yǔ)法速查及實(shí)例說(shuō)明
隨機(jī)閱讀
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 01-10C#中split用法實(shí)例總結(jié)
- 01-10delphi制作wav文件的方法
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 04-02jquery與jsp,用jquery
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文