Solved

VBScript : Convert text : "ASCII Codes" to "ISO 8859-1 Characters"

Posted on 2011-03-21
2
1,580 Views
Last Modified: 2012-05-11
Hi there,

The purpose of this script is to convert some characters within a text file.

The problem here is, for example, once it converted &H88 to &HEA, it will reconvert the same character from &HEA to &HDB.

I need this script to convert a character only once.

(I was wondering if the use of a "regular expression" would be the answer.)

Please provide full code.
 
Thanks for your help,
Rene

 
Dim Mystring,MyFile
MyFile=WScript.Arguments(0)

'get file in string
Mystring=Getfile(MyFile)

MyString=replace(MyString,chr(&HB7),chr(&HC0))
MyString=replace(MyString,chr(&H85),chr(&HE0))
MyString=replace(MyString,chr(&HB6),chr(&HC2))
MyString=replace(MyString,chr(&H83),chr(&HE2))
MyString=replace(MyString,chr(&H92),chr(&HC6))
MyString=replace(MyString,chr(&H91),chr(&HE6))
MyString=replace(MyString,chr(&H80),chr(&HC7))
MyString=replace(MyString,chr(&H87),chr(&HE7))
MyString=replace(MyString,chr(&HD4),chr(&HC8))
MyString=replace(MyString,chr(&H8A),chr(&HE8))
MyString=replace(MyString,chr(&H90),chr(&HC9))
MyString=replace(MyString,chr(&H82),chr(&HE9))
MyString=replace(MyString,chr(&HD2),chr(&HCA))
MyString=replace(MyString,chr(&H88),chr(&HEA))
MyString=replace(MyString,chr(&HD3),chr(&HCB))
MyString=replace(MyString,chr(&H89),chr(&HEB))
MyString=replace(MyString,chr(&HD7),chr(&HCE))
MyString=replace(MyString,chr(&H8C),chr(&HEE))
MyString=replace(MyString,chr(&HD8),chr(&HCF))
MyString=replace(MyString,chr(&H8B),chr(&HEF))
MyString=replace(MyString,chr(&HE2),chr(&HD4))
MyString=replace(MyString,chr(&H93),chr(&HF4))
MyString=replace(MyString,chr(&HEB),chr(&HD9))
MyString=replace(MyString,chr(&H97),chr(&HF9))
MyString=replace(MyString,chr(&HEA),chr(&HDB))
MyString=replace(MyString,chr(&H96),chr(&HFB))
MyString=replace(MyString,chr(&H9A),chr(&HDC))
MyString=replace(MyString,chr(&H81),chr(&HFC))


'write result in same file
 Dim OutStream, FS
 on error resume Next
 Set FS = CreateObject("Scripting.FileSystemObject")
 Set OutStream = FS.OpenTextFile(MyFile, 2, True)
 OutStream.Write MyString

function GetFile(FileName)
  If FileName<>"" Then
    Dim FS, FileStream
    Set FS = CreateObject("Scripting.FileSystemObject")
      on error resume Next
      Set FileStream = FS.OpenTextFile(FileName)
      GetFile = FileStream.ReadAll
  End If
End Function

Open in new window

0
Comment
Question by:ReneGe
2 Comments
 
LVL 1

Accepted Solution

by:
B34RDY earned 500 total points
Comment Utility
I have altered your code slightly so that it won't change a character and then change it again.
It is to do with the order in which they are checked. If you look at my change you will see I have made 2 repositions of your replace code.


Dim Mystring,MyFile
MyFile=WScript.Arguments(0)

'get file in string
Mystring=Getfile(MyFile)

MyString=replace(MyString,chr(&HB7),chr(&HC0))
MyString=replace(MyString,chr(&H85),chr(&HE0))
MyString=replace(MyString,chr(&HB6),chr(&HC2))
MyString=replace(MyString,chr(&H83),chr(&HE2))
MyString=replace(MyString,chr(&H92),chr(&HC6))
MyString=replace(MyString,chr(&H91),chr(&HE6))
MyString=replace(MyString,chr(&H80),chr(&HC7))
MyString=replace(MyString,chr(&H87),chr(&HE7))
MyString=replace(MyString,chr(&HD4),chr(&HC8))
MyString=replace(MyString,chr(&H8A),chr(&HE8))
MyString=replace(MyString,chr(&H90),chr(&HC9))
MyString=replace(MyString,chr(&H82),chr(&HE9))
MyString=replace(MyString,chr(&HD2),chr(&HCA))
MyString=replace(MyString,chr(&HEA),chr(&HDB))
MyString=replace(MyString,chr(&H88),chr(&HEA))
MyString=replace(MyString,chr(&HD3),chr(&HCB))
MyString=replace(MyString,chr(&HEB),chr(&HD9))
MyString=replace(MyString,chr(&H89),chr(&HEB))
MyString=replace(MyString,chr(&HD7),chr(&HCE))
MyString=replace(MyString,chr(&H8C),chr(&HEE))
MyString=replace(MyString,chr(&HD8),chr(&HCF))
MyString=replace(MyString,chr(&H8B),chr(&HEF))
MyString=replace(MyString,chr(&HE2),chr(&HD4))
MyString=replace(MyString,chr(&H93),chr(&HF4))
MyString=replace(MyString,chr(&H97),chr(&HF9))
MyString=replace(MyString,chr(&H96),chr(&HFB))
MyString=replace(MyString,chr(&H9A),chr(&HDC))
MyString=replace(MyString,chr(&H81),chr(&HFC))


'write result in same file
 Dim OutStream, FS
 on error resume Next
 Set FS = CreateObject("Scripting.FileSystemObject")
 Set OutStream = FS.OpenTextFile(MyFile, 2, True)
 OutStream.Write MyString

function GetFile(FileName)
  If FileName<>"" Then
    Dim FS, FileStream
    Set FS = CreateObject("Scripting.FileSystemObject")
      on error resume Next
      Set FileStream = FS.OpenTextFile(FileName)
      GetFile = FileStream.ReadAll
  End If
End Function

Open in new window

0
 
LVL 10

Author Comment

by:ReneGe
Comment Utility
Got it!

I found 2 more to reorder. Tested it and works well.

Thanks Tuyau2poil for writing script.
Thanks B34RDY for helping me resolving the issue.

Cheers,
Rene
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now