Notes about VBScript

Little intro: Visual Basic Script is a one of Visual Basic dialects developed/used by Microsoft. It comes with any modern Windows and in complex with Windows Scripting Host (which giving access to some system APIs) was added to code some automation stuff and replace old quirky less-programs-than-unix-shell .bat-files and later was substituted (I think) by Powershell.

Back in a days, you know, vbscript was also used in IE (when it was common, of course) just like JS (MS used their super/sub-set called JScript). And it was there, when XMLHttpRequest has appeared, so we have ability to do HTTP requests from vbs.

This example fetches json and extracts quoted strings (I’m too dumb for real parser)

Dim response
Dim mode
Dim start
mode = 0
Set xhr = CreateObject("MSXML2.XMLHTTP")
xhr.open "GET", "https://example.com/uri_givin_me_json", False
xhr.send
response = xhr.responseText
i = 1
While i < Len(response)
    char = Mid(response, i, 1)
    Select case char
        Case """"
            If mode = 0 Then
                mode = 1
                start = i + 1
            Else
                WScript.StdOut.Write(Mid(response, start, i - start)) & vbcrlf
                mode = 0
            End If
        Case "\"
            i = i + 1
    End Select
    i = i + 1
Wend

Another fact about vbs is that it doesn’t have (or i haven’t found) primitive dictionary/map structure and dynamic arrays. This example parses urlencoded form (not in php compliant way) and produces Scripting.Dictionary object

Function UELoad(s)
    Set retval = CreateObject("Scripting.Dictionary")
    pairs = Split(s, "&")
    For Each p in pairs
        kv = Split(p, "=")
        If retval.Exists(kv(0)) Then
            If VarType(retval.Item(kv(0))) = vbString Then
                a = array(2)
                a(0) = retval.Item(kv(0))
                a(1) = kv(1)
                retval.Key(kv(0)) = a
            Else
                a = retval.Item(kv(0))
                n = UBound(a)
                ReDim Preserve a(n + 1) 'resizing array, who has said "realloc"
                a(n) = kv(1)
                retval.Key(kv(0)) = a
            End If
        Else
            retval.Add kv(0), kv(1)
        End If
    Next
    Set UELoad = retval 'weird a bit, must assign return value to function name
End Function

Set x = UELoad("foo=1&bar=2")
WScript.Echo x.Item("foo")