Copy a Unicode File to an ANSI File

The VBScript file WiToAnsi.vbs is provided in the Windows SDK Components for Windows Installer Developers. This sample shows how script is used to rewrite a Unicode text file as an ANSI text file.

Using this sample requires the CScript.exe or WScript.exe version of Windows Script Host. To use CScript.exe to run this sample, type a command line at the command prompt using the following syntax. Help is displayed if the first argument is /? or if too few arguments are specified. To redirect the output to a file, end the command line with VBS > [path to file]. The sample returns a value of 0 for success, 1 if help is invoked, and 2 if the script fails.

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

Specify the Unicode text file that is being converted. Specify the ANSI text file that is being created. If no ANSI file is specified, the Unicode file is replaced.

For additional scripting examples, see Windows Installer Scripting Examples. For sample utilities that do not require Windows Script Host see Windows Installer Development Tools.

Send comments about this topic to Microsoft

Build date: 11/12/2009

Tags :


Community Content

Thomas Lee
WiToAnsi - script in VB
' 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) Microsoft Corporation. All rights reserved.
'
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" &_
vbNewLine &_
vbNewLine & "Copyright (C) Microsoft Corporation. All rights reserved."
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

Tags : code vbscript

Thomas Lee
Copy to Unicode - written in PowerShell
# 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) Thomas Lee tfl@psp.co.uk. All rights reserved.
# Script is slighly simpler than VBS one!


# Script takes two arguments
Param ($UnicodeFile = "C:\foo\unicodefile.txt",
$ansiFile = "C:\foo\asciifile.txt",
$help=$false)

# FileSystemObject.CreateTextFile and FileSystemObject.OpenTextFile
$OpenAsASCII = 0
$OpenAsUnicode = -1

# FileSystemObject.CreateTextFile
$OverwriteIfExist = -1
$FailIfExist = 0

# FileSystemObject.OpenTextFile
$OpenAsDefault = -2
$CreateIfNotExist = -1
$FailIfNotExist = 0
$ForReading = 1
$ForWriting = 2
$ForAppending = 8


If ($help) {
"Utility to copy Unicode text file to an ANSI text file."
"The 1st argument is the Unicode text file to read"
"The 2nd argument is the ANSI text file to write"
"If the 2nd argument is omitted, the Unicode file will be replaced"
"Copyright (C) Thomas Lee - tfl@psp.co.uk. All rights reserved."
}

# create the objects (open the files)
$FileSys = new-object -com Scripting.FileSystemObject
$inStream = $FileSys.OpenTextFile($UnicodeFile, $ForReading, $FailIfNotExist, $OpenAsDefault)
$outStream = $FileSys.CreateTextFile($ansifile, $OverwriteIfExist, $OpenAsASCII)
$i=0

£ now do the copy
Do {
$inLine = $inStream.ReadLine()
$outStream.WriteLine($inLine)
$i++
}
Until ($inStream.AtEndOfStream)
"$I lines copied"
$instream.Close()
$outStream.Close()


Sly Gryphon
PowerShell redux
PS> Get-Content infile.txt -encoding Unicode | Set-Content outfile.txt -encoding ASCII

or you can even leave off the source encoding (automatically detected) and the destination encoding (default is ASCII)

PS> gc infile.txt | sc outfile.txt

(Note one minor caveat - that Get-Content/Set-Content process a line at a time, and so will add a CRLF to the last line if it does not already have one.)
Tags :

Page view tracker