溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

vbs實現(xiàn)unicode和ascii編碼轉(zhuǎn)換的方法教程

發(fā)布時間:2021-09-28 16:22:54 來源:億速云 閱讀:579 作者:iii 欄目:開發(fā)技術(shù)

本篇內(nèi)容主要講解“vbs實現(xiàn)unicode和ascii編碼轉(zhuǎn)換的方法教程”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學(xué)習(xí)“vbs實現(xiàn)unicode和ascii編碼轉(zhuǎn)換的方法教程”吧!

一、Copy a Unicode File to an ANSI File

WiToAnsi.vbs文件:

復(fù)制代碼 代碼如下:


' 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)用:

復(fù)制代碼 代碼如下:


cscript WiToAnsi.vbs [path to Unicode file][path to ANSI file]

二、Copy a ANSI File to an Unicode File

只需對OpenTextFile和CreateTextFile的打開方式做調(diào)整即可。

三、參考

http://msdn.microsoft.com/en-us/library/aa368046%28VS.85%29.aspx

四、OpenTextFile和CreateTextFile的使用

CreateTextFile 方法

創(chuàng)建指定文件并返回 TextStream 對象,該對象可用于讀或?qū)憚?chuàng)建的文件。

復(fù)制代碼 代碼如下:


object.CreateTextFile(filename[, overwrite[, unicode]])

參數(shù)

object

必選項。應(yīng)為 FileSystemObject 或 Folder 對象的名稱。

filename

必選項。字符串表達式,指明要創(chuàng)建的文件。

overwrite

可選項。Boolean 值指明是否可以覆蓋現(xiàn)有文件。如果可覆蓋文件,該值為 True;如果不能覆蓋文件,則該值為 False 。如果省略該值,則不能覆蓋現(xiàn)有文件。

unicode

可選項。Boolean 值指明是否以 Unicode 或 ASCII 文件格式創(chuàng)建文件。如果以 Unicode 文件格式創(chuàng)建文件,則該值為 True;如果以 ASCII 文件格式創(chuàng)建文件,則該值為 False。如果省略此部分,則假定創(chuàng)建 ASCII 文件。

OpenTextFile 方法

打開指定的文件并返回一個 TextStream 對象,可以讀取、寫入此對象或?qū)⑵渥芳拥轿募?br/>

復(fù)制代碼 代碼如下:


object.OpenTextFile(filename[, iomode[, create[, format]]])

參數(shù)

object

必選項。應(yīng)為 FileSystemObject 對象的名稱。

filename

必選項。字符串表達式,指明要打開的文件名稱。

iomode

可選項。輸入/輸出模式,是下列三個常數(shù)之一:ForReading,F(xiàn)orWriting,或 ForAppending。

create

可選項。Boolean 值,指出當(dāng)指定的 filename 不存在時是否能夠創(chuàng)建新文件。允許創(chuàng)建新文件時為 True,否則為 False。默認(rèn)值為 False。

format

可選項。三個 Tristate 值之一,指出以何種格式打開文件。若忽略此參數(shù),則文件以 ASCII 格式打開。

設(shè)置

iomode 參數(shù)可為下列設(shè)置之一:

數(shù)描述
ForReading1以只讀模式打開文件。不能對此文件進行寫操作。
ForWriting2以只寫方式打開文件。不能對此文件進行讀操作。
ForAppending8打開文件并在文件末尾進行寫操作。

format 參數(shù)可為下列設(shè)置之一:

常數(shù)描述
TristateUseDefault-2以系統(tǒng)默認(rèn)格式打開文件。
TristateTrue-1以 Unicode 格式打開文件。
TristateFalse 0以 ASCII 格式打開文件。

到此,相信大家對“vbs實現(xiàn)unicode和ascii編碼轉(zhuǎn)換的方法教程”有了更深的了解,不妨來實際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進入相關(guān)頻道進行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI