2

I must create a text file with ANSI encoding using vbs.

If I just create it, without to write any text, using this code...

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set ObjFileTxt = objFSO.objFSO.CreateTextFile("filename.txt", True, False)  
Set ObjFileTxt = nothing
Set objFSO = nothing

... I get an ANSI text file, but if I try to write some text, for example...

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set ObjFileTxt = objFSO.CreateTextFile("filename.txt", True, False)   
ObjFileTxt.Write "ok"
Set ObjFileTxt = nothing
Set objFSO = nothing

... I get an UTF8 text file.

Any help?

  • 2
    How have you determined that the file is UTF8? – MC ND Nov 30 '16 at 12:00
  • Hello, I opened the file with Notepad++ and I checked its format – Gennaro Lippiello Nov 30 '16 at 12:06
  • 1
    Strange I don't see how that can be the case to be honest, are you checking it in Notepad++ correctly? Have you looked at the Hex to confirm it? – user692942 Nov 30 '16 at 12:13
  • like that you can't IAA, you need another method to create your file, see http://stackoverflow.com/questions/5182102/vb6-vbscript-change-file-encoding-to-ansi – peter Nov 30 '16 at 12:26
  • 3
    @GennaroLippiello, it is not your file, it is Notepad++. If you change the format to ANSI in Notepad++, save the file, close it and open again, the editor will identify it as UTF8 as it is its default encoding and there is nothing in the file contradicting the assumption. Read the Ansgar Wiechers [answer](http://stackoverflow.com/a/40888255/2861476) for more information. – MC ND Nov 30 '16 at 12:34

2 Answers2

4

It's not possible that the code you posted would create a UTF8-encoded output file. The CreateTextFile method supports only ANSI (windows-1252) and Unicode (little endian UTF-16) encodings.

The only ways to create UTF8-encoded files in VBScript are the ADODB.Stream object:

Set stream = CreateObject("ADODB.Stream")
stream.Open
stream.Type     = 2 'text
stream.Position = 0
stream.Charset  = "utf-8"
stream.WriteText "some text"
stream.SaveToFile "C:\path\to\your.txt", 2
stream.Close

or writing the individual bytes (including the BOM).

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
0

I know this is already old, but for future references...
Believe it or not, I had the same problem until I converted my VBS to ANSI (was UTF-8). Then, every new text file created with it was also ANSI and not UTF-8 any more.

  • It makes no difference if the output contains non-Unicode characters such as the "ok" string in the example. The file will be binary identical if it's encoded as ANSI or UTF-8. However, if your script contains a Unicode character, such as , and you save your script as UTF-8 encoded, writing that character to a text file (not using ADODB) will indeed create a UTF-8 output file containing . Which appears to contradict the accepted answer, but use caution because, typically, to work successfully with Unicode characters in VBScript, the script and output files should be UTF-16 encoded. – LesFerch Apr 05 '22 at 02:04
  • Yes, confirmed. A UTF-8 encoded script will write a UTF-8 encoded text file using `CreateTextFile`. However `ADODB.Stream` must be used to read it back as UTF-8. – LesFerch Apr 05 '22 at 02:12