Q:

VBscript - Transpose CSV File

Does anyone have a short script in VBscript for transposing a Matrix (given as CSV (comma separated values) file)? 
A, 1, 2, 3
B, 7, 5, 6

-> 
A, B
1, 7
2, 5
3, 6

Many Thanks in advance 
Tom

A:

So by creating dynamic arrays and auto-increment their growth in parallel with discovering new columns of the original matrix, you can auto build the new data structure quite quickly.  

Const OutputCSV = "C:\op.csv"
Dim dt_start, WriteOutput : dt_start = Now
Dim fso : Set fso = CreateObject("Scripting. FileSystemObject")
Dim file : Set file = fso. OpenTextFile("C:\test.csv", 1, True)
Set WriteOutput = fso. OpenTextFile(OutputCSV, 8, True)
Dim fc : fc = file. ReadAll : file.close : Dim fcArray : fcArray = Split(fc, vbCrLf)
WScript.echo "Before Transpose"
WScript.echo "----------------"
WScript.echo fc
WScript.echo "----------------"
Dim opArray() : ReDim opArray(0)
For Each row In fcArray
    Dim tmp: tmp = Split(row, ",")
    For ent=0 To UBound(tmp)
        If ent  > UBound(opArray) Then
            ReDim Preserve opArray(UBound(opArray)+1)
            opArray(ent) = Trim(tmp(ent))
        Else
            If Len(opArray(ent)) > 0 Then
                opArray(ent) = opArray(ent) & "," & Trim(tmp(ent))
            Else
                opArray(ent) = Trim(tmp(ent))
            End If
        End If
    Next
Next
Dim dt_end : dt_end = Now
WScript.echo "After Transpose"
WScript.echo "----------------"
WScript.echo Join(opArray, vbCrLf)
WScript.echo "----------------"
WScript.echo "Script Execution Time (sec): " & DateDiff("s", dt_start, dt_end)
WriteOutput. Write Join(opArray, vbCrLf) : WriteOutput. Close

