Search code examples
asp-classicvbscript

UTF-8 file appending in vbscript/classicasp - can it be done?


My current knowledge:

If you are trying to write text files in vbscript / asp land you have two options.

  1. the Scripting.FileSystemObject
  2. the ADODB.Stream object

Scripting.FileSystemObject does not support utf8. It will write in Ascii, or Unicode (and waste two bytes for most of your characters)

ADODB.Stream does not support appending (afaik). I can't figure out how to make the ADODB.Stream object actually open a file and write to it when I call stream.Write. There is a SaveToFile function, but it outputs the entire stream.

If you need to write 1GB files, you would have to fill the whole 1GB into the stream before you could write it out.

Is there a special trick to get the ADODB.Stream object to link to a file on disk? I tried :

stream.Open "URL=file:///c:\test.txt"

but that gave an error.


Solution

  • In this scenario, I would probably create a COM component that takes a string, and runs it through WideCharToMultiByte to convert it to UTF-8.

    In case you really want to stay within VBScript, I just hacked up a very quick and very dirty UTF-8 conversion...

    Function Utf8(ByVal c)
    
        Dim b1, b2, b3
    
        If c < 128 Then
            Utf8 = chr(c)
        ElseIf c < 2048 Then
            b1 = c Mod 64
            b2 = (c - b1) / 64
            Utf8 = chr(&hc0 + b2) & chr(&h80 + b1)
        ElseIf c < 65536 Then
            b1 = c Mod 64
            b2 = ((c - b1) / 64) Mod 64
            b3 = (c - b1 - (64 * b2)) / 4096
            Utf8 = chr(&he0 + b3) & chr(&h80 + b2) & chr(&h80 + b1)
        End If
    
    End Function
    

    Just open a file with Scripting.FileSystemObject, using system default encoding. Then pass every character of the string you want to write through this function.

    NOTE that the function expects a Unicode code point, so be sure to use AscW() instead of plain Asc():

    For i = 1 to Len(s)
        file.Write Utf8(AscW(Mid(s, i, 1)))
    Next