VBS implements UNICODE and ASCII encoding conversions _vbs

Source: Internet
Author: User

A, Copy a Unicode file to an ANSI file

Witoansi.vbs file:

Copy Code code as follows:

' Utility to rewrite a Unicode text file as a 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 would 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"
    S ET WshShell = WScript.CreateObject ("Wscript.Shell")
    wshshell.run "cmd.exe/c copy" & OutFile & Amp "" & 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)
Todo
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

Called in batch processing:

Copy Code code as follows:

cscript witoansi.vbs [path to Unicode File][path to ANSI file]

Second, Copy a ANSI file to a Unicode file

Just make adjustments to the OpenTextFile and CreateTextFile open mode.

Third, the reference

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

Iv. use of OpenTextFile and CreateTextFile

CreateTextFile method

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

Copy Code code as follows:

Object. CreateTextFile (filename[, overwrite[, Unicode])

Parameters

Object

Required option. Should be the name of the FileSystemObject or Folder object.

FileName

Required option. A string expression that indicates the file to create.

Overwrite

Options available. A Boolean value that indicates whether an existing file can be overwritten. True if the file can be overwritten, or False if the file cannot be overwritten. If this value is omitted, the existing file cannot be overwritten.

Unicode

Options available. A Boolean value that indicates whether the file is created in Unicode or ASCII file format. True If the file is created in a Unicode file format, False if the file is created in an ASCII file format. If this section is omitted, the ASCII file is assumed to be created.

OpenTextFile method

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

Copy Code code as follows:

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

Parameters

Object

Required option. Should be the name of the FileSystemObject object.

FileName

Required option. A string expression that indicates the name of the file to open.

IOMode

Options available. The input/output mode is one of the following three constants: Forreading,forwriting, or ForAppending.

Create

Options available. Boolean value that indicates 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

Options available. One of three tristate values that indicates in what format the file is opened. If this argument is omitted, the file is opened in ASCII format.

Set up

The IOMode parameter can be one of the following settings:

number value Description
ForReading 1 Open the file in read-only mode. This file cannot be written to.
ForWriting 2 Opens the file as a write-only method. 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:

Constants value Description
Tristateusedefault -2 Open the file in the system default format.
TristateTrue -1 Opens the file in Unicode format.
Tristatefalse 0 Opens the file in ASCII format.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.