i\'m using vbscript to extract data from db2 and write to file. Writing to file like:
Set objTextFile = objFSO.CreateTextFile(sFilePath, True, True)
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"
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