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

Jerold Schulman

January 29, 2006

2 Min Read
ITPro Today logo in a gray background | ITPro Today


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:utiltest7.vbs, the console displayed:

CN=Jerold Schulman,CN=Users,DC=JSIINC,DC=COM    |  Jerry     |  [email protected]=Jennifer Schulman,CN=Users,DC=JSIINC,DC=COM  |  Jennifer  |  [email protected]=Jane Doe,CN=Users,DC=JSIINC,DC=COM           |  Jane.Doe  |  [email protected]

My test7.vbs script contained:

Dim txtOn Error Resume NextSet objUser = GetObject("LDAP://CN=Jerold Schulman,CN=Users,DC=JSIINC,DC=COM")txt=objUser.distinguishedName & "|" & objUser.sAMAccountName & "|" & objUser.userPrincipalName & vbCrlFSet objUser = GetObject("LDAP://CN=Jennifer Schulman,CN=Users,DC=JSIINC,DC=COM")txt=txt & objUser.distinguishedName & "|" & objUser.sAMAccountName & "|" & objUser.userPrincipalName & vbCrlFSet objUser = GetObject("LDAP://CN=Jane Doe,CN=Users,DC=JSIINC,DC=COM")txt=txt & objUser.distinguishedName & "|" & objUser.sAMAccountName & "|" & objUser.userPrincipalName & vbCrlFWscript.Echo Make_Columns(txt, "|", "  ")Function Make_Columns(stringTEXT, deMark, leadTrailSpace)Dim arrayCOL, elementCOL, strElement, elementROWDim arrayLEN, elementLEN, arrayROW, stringROWDim stringProportional, proportionalColumns, numberMake_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 = proportionalColumnsEnd Function



Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like