VBScript fputcsv function

A VBScript equivalent of PHP’s fputcsv


Public function fputcsv(fields,delimiter,enclosure)

    fputcsv = false
    If len(delimiter) = 0 Then delimiter = ","
    If len(enclosure) = 0 Then enclosure = ""

    Dim key,replaced
    For key = 0 to uBound(fields)
        replaced = false
        If inStr(fields(key),delimiter) or inStr(fields(key),enclosure) or inStr(fields(key),vbCrLf) Then
            fields(key) = Replace(fields(key),enclosure,enclosure & enclosure)
            fields(key) = enclosure & fields(key) & enclosure
        End If
    Next

    Dim str : str = join(fields,delimiter)
    ts.WriteLine str
    fputcsv = len(str)
end function

Please also note that php.vbs offers community built functions and goes by the McDonald’s Theory. We’ll put online functions that are far from perfect, in the hopes to spark better contributions. Do you have one? Then please just:

Other PHP functions in the filesystem extension