Ruslan
Ruslan

Reputation: 319

Unicode to UTF-8

i'm using vbscript to extract data from db2 and write to file. Writing to file like:

Set objTextFile = objFSO.CreateTextFile(sFilePath, True, True)

that creates file in unicode. But that is xml file and it uses UTF-8. So when i open xml file with MS XML Notepad it throws error: 'hexadecimal value 0x00 is an invalid character'

So i opening this text file with TextPad and saving in UTF-8. After that XML opens without any problems. Can i convert file from Unicode to UTF-8 by vbScript?

Upvotes: 8

Views: 29201

Answers (2)

Adilson Furlani
Adilson Furlani

Reputation: 5

Well, in some cases, we need to do this in WSH in a machine without ADO. In this case, keep in your mind that WSH don't create file in UTF-8 format (CreateTextFile method not work with UTF-8), but is completely possible to manipulate an UTF-8 file (appending data). Thinking this, I found an non-orthodoxal solution. Follow this steps:

1) Open a blank NOTEPAD, click FILE > SAVE AS, type a name for the file (like UTF8FileFormat.txt, per example), change the field "Encoding" to UTF-8 and click in [Save]. Leave NOTEPAD.

2) In your WSH you will use the UTF8FileFormat.txt to create your UTF8 text file. To do this, after your FileSystemObject declaration, use the CopyFile method to copy the UTF8FileFormat.txt to a new file (remember to use the Overwrite option) and, then, use the OpenTextFile method to open your new file with ForAppending and NoCreate options. After this, you will can write in this file normally (as in CreateTextFile method). Your new file will be in UTF-8 format. Below follow an example:

'### START
'  ### REMEMBER: You need to create the UTF8FileFormat.txt file in a blank
'  ###           NOTEPAD with UTF-8 Encoding first.
   Unicode=-1 : ForAppending=8 : NoCreate=False : Overwrite=True
   set fs = CreateObject("Scripting.FileSystemObject")
   fs.CopyFile "UTF8FileFormat.txt","MyNewUTF8File.txt",Overwrite
   set UTF8 = fs.OpenTextFile("MyNewUTF8File.txt", ForAppending, NoCreate)
   UTF8.writeline "My data can be writed in UTF-8 format now"
   UTF8.close
   set UTF8 = nothing
'### END

Upvotes: -2

stealthyninja
stealthyninja

Reputation: 10371

Using the Stream object to save your file with the utf-8 charset might work better for you; here's a simple .vbs function you could test out on your data:

Option Explicit

Sub Save2File (sText, sFile)
    Dim oStream
    Set oStream = CreateObject("ADODB.Stream")
    With oStream
        .Open
        .CharSet = "utf-8"
        .WriteText sText
        .SaveToFile sFile, 2
    End With
    Set oStream = Nothing
End Sub

' Example usage: '
Save2File "The data I want in utf-8", "c:\test.txt"

Upvotes: 16

Related Questions