how to creat a utf-8 without bom textfile by VBS?

Discussion in 'Scripting' started by jonathan_hzs, Jan 18, 2023.

  1. jonathan_hzs

    jonathan_hzs MDL Novice

    Feb 3, 2021
    30
    5
    0
    Anyone can explain me a method to save a file in UTF-8 without BOM, all i've tested save in UTF-8 with BOM.
     
  2. dotMDLF

    dotMDLF MDL Novice

    Jul 20, 2015
    5
    9
    0
    Sample:
    Code:
    Option Explicit
    
    Dim sTEXT,sFILE
    
    sFILE = "TEST_UTF8_NOBOM.TXT"
    sTEXT = "Сампле тест"
    
    Call WriteFileUTF8_NOBOM(sFILE,sTEXT)
    
    WScript.Echo(sTEXT)
    
    Private Sub WriteFileUTF8_NOBOM(sFILE,sTEXT)
        Const adSaveCreateNotExist = 1
        Const adSaveCreateOverWrite = 2
        Const adTypeBinary = 1
        Const adTypeText = 2
        Const adModeReadWrite = 3
    
        Dim oUTF8Stream,oBinaryStream
        Set oUTF8Stream = CreateObject("ADODB.Stream")
        Set oBinaryStream = CreateObject("ADODB.Stream")
    
        oUTF8Stream.Type = adTypeText
        oUTF8Stream.Charset = "utf-8"
        oUTF8Stream.Open
        oUTF8Stream.WriteText sTEXT
        oUTF8Stream.Position = 3 ' Skip BOM
    
        oBinaryStream.Type = adTypeBinary
        oBinaryStream.Mode = adModeReadWrite
        oBinaryStream.Open
    
        oUTF8Stream.CopyTo oBinaryStream
        oUTF8Stream.Flush
        oUTF8Stream.Close
    
        oBinaryStream.SaveToFile sFILE, adSaveCreateOverWrite
        oBinaryStream.Flush
        oBinaryStream.Close
    
        Set oUTF8Stream = Nothing
        Set oBinaryStream = Nothing
    End Sub
    
    
     
  3. jonathan_hzs

    jonathan_hzs MDL Novice

    Feb 3, 2021
    30
    5
    0
    cool,thanks a lot!