Skip navigation

JSI Tip 10103. How can my VBScript output information in aligned columns?


I have scripted the Make_Columns function to output a string that has a specified column separator and CrLf at the end of each line to the console in a tidy column format.

The function has 3 parameters:

StringTEXT     contains the text to be formatted in columns.

deMark         contains the separator, like "|". It can be a string of characters.

leadTrailSpace contains the spaces that you wish to add before and after the deMark, like "  ".
NOTE: Leading and trailing spaces are eliminated before applying leadTrailSpace.

When I typed cscript //nologo c:\util\test7.vbs, the console displayed:

CN=Jerold Schulman,CN=Users,DC=JSIINC,DC=COM    |  Jerry     |  [email protected]
CN=Jennifer Schulman,CN=Users,DC=JSIINC,DC=COM  |  Jennifer  |  [email protected]
CN=Jane Doe,CN=Users,DC=JSIINC,DC=COM           |  Jane.Doe  |  [email protected]
My test7.vbs script contained:
Dim txt
On Error Resume Next
Set objUser = GetObject("LDAP://CN=Jerold Schulman,CN=Users,DC=JSIINC,DC=COM")
txt=objUser.distinguishedName & "|" & objUser.sAMAccountName & "|" & objUser.userPrincipalName & vbCrlF
Set objUser = GetObject("LDAP://CN=Jennifer Schulman,CN=Users,DC=JSIINC,DC=COM")
txt=txt & objUser.distinguishedName & "|" & objUser.sAMAccountName & "|" & objUser.userPrincipalName & vbCrlF
Set objUser = GetObject("LDAP://CN=Jane Doe,CN=Users,DC=JSIINC,DC=COM")
txt=txt & objUser.distinguishedName & "|" & objUser.sAMAccountName & "|" & objUser.userPrincipalName & vbCrlF
Wscript.Echo Make_Columns(txt, "|", "  ")

Function Make_Columns(stringTEXT, deMark, leadTrailSpace)
Dim arrayCOL, elementCOL, strElement, elementROW
Dim arrayLEN, elementLEN, arrayROW, stringROW
Dim stringProportional, proportionalColumns, number
Make_Columns = ""
proportionalColumns = ""
'Set each column widths to widest value.
    arrayROW = Split(stringTEXT,vbCrLf)
    For elementROW = 0 To UBound(arrayROW)
        stringROW = arrayROW(elementROW)
        arrayCOL = Split(stringROW,deMark)
        If elementROW = 0 Then
            ReDim arrayLEN(UBound(arrayCOL))
            For elementLEN = 0 To UBound(arrayLEN)
                arrayLEN(elementLEN) = 0
            Next
        End If
        For elementCOL = 0 To UBound(arrayCOL)
            strElement = Trim(arrayCOL(elementCOL))
            If Len(strElement) > arrayLEN(elementCOL) Then
                arrayLEN(elementCOL) = Len(strElement)
            End If
        Next
    Next
'Execute spacing.
    For elementROW = 0 To UBound(arrayROW)
        stringROW = arrayROW(elementROW)
        arrayCOL = Split(stringROW,"|")
        For elementCOL = 0 To UBound(arrayCOL)
            strElement = Trim(arrayCOL(elementCOL))
            number = arrayLEN(elementCOL) - Len(strElement)    
            stringProportional = Space(number)
            If elementCOL = 0 Then 
                proportionalColumns = proportionalColumns & strElement & stringProportional
            Else
                proportionalColumns = proportionalColumns & leadTrailSpace & deMark & leadTrailSpace & strElement & stringProportional
            End If
        Next
        proportionalColumns = proportionalColumns & vbCrLf
    Next
    Make_Columns = proportionalColumns
End Function



Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish