Encoding conversion between Unicode and ASCII using vbs

Time:2022-1-3

1、 Copy a Unicode file to an ANSI file

WiToAnsi. VBS file:

 

Copy codeThe code is as follows:


‘ 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

 

Call in batch:

Copy codeThe code is as follows:

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

 

2、 Copy a ANSI file to an Unicode file

Just adjust the opening mode of opentextfile and createtextfile.

3、 Reference

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

4、 Use of opentextfile and createtextfile

Createtextfile method

Creates the specified file and returns a TextStream object that can be used to read or write the created file.

Copy codeThe code is as follows:

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

 

parameter

object

Required. Expected the name of the FileSystemObject or folder object.

filename

Required. String expression indicating the file to create.

overwrite

Optional. The Boolean value indicates whether an existing file can be overwritten. This value is true if the file can be overwritten; If the file cannot be overwritten, the value is false. If this value is omitted, the existing file cannot be overwritten.

unicode

Optional. The Boolean value indicates whether the file is created in Unicode or ASCII file format. This value is true if the file is created in Unicode file format; This value is false if the file is created in ASCII file format. If this section is omitted, it is assumed that an ASCII file is created.

Opentextfile method

Opens the specified file and returns a TextStream object that can be read, written, or appended to the file.

Copy codeThe code is as follows:

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

 

parameter

object

Required. Expected the name of the FileSystemObject object.

filename

Required. String expression indicating the name of the file to open.

iomode

Optional. The input / output mode is one of the following three constants: forreading, forwriting, or forappending.

create

Optional. Boolean value indicating whether a new file can be created when the specified filename does not exist. True when new files are allowed to be created, otherwise false. The default value is false.

format

Optional. One of three tristate values indicating the format in which the file is opened. If this parameter is ignored, the file opens in ASCII format.

set up

The iomode parameter can be one of the following settings:

 

number

value

describe

ForReading

1

Open the file in read-only mode. Cannot write to this file.

ForWriting

2

Open the file in write only mode. This file cannot be read.

ForAppending

8

Open the file and write at the end of the file.

 

The format parameter can be one of the following settings:

 

constant

value

describe

TristateUseDefault

-2

Open the file in the system default format.

TristateTrue

-1

Open the file in Unicode format.

TristateFalse

 0

Open the file in ASCII format.