WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Strings

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Convert Tab Delimited Text to Fixed Width Text

 Keywords: Convert tab delimited text to fixed columns 

;---------------------------------------------------------------------------------------------
; Convert tab delimited text to fixed columns
; Text = one or more tab-delimited lines
; AlignStr = String of letters to control column alignment: L=Left, R=Right, C=Center
; Les Ferch, 2008-03-10

#DefineFunction Tab2Fixed(Text,AlignStr)
Text = StrReplace(Text,@CRLF,@LF)
LineCount = ItemCount(Text,@LF)
FirstLine = ItemExtract(1,Text,@LF)
ColumnCount = ItemCount(FirstLine,@TAB)
MaxWidth = ArrDimension(ColumnCount)
Alignment = ArrDimension(ColumnCount)
ArrInitialize(MaxWidth,0)
For i = 1 To ColumnCount
   A = StrSub(AlignStr,i,1)
   If StrScan(A,"LRC",1,@FWDSCAN)==0 Then A = "L"
   Alignment[i - 1] = A
Next
For i = 1 To LineCount
   Line = ItemExtract(i,Text,@LF)
   For j = 1 To ColumnCount
      k = j - 1
      Item = ItemExtract(j,Line,@TAB)
      MaxWidth[k] = Max(MaxWidth[k],StrCharCount(Item))
   Next
Next
NewText = ""
For i = 1 To LineCount
   Line = ItemExtract(i,Text,@LF)
   NewLine = ""
   For j = 1 To ColumnCount
      k = j - 1
      Item = ItemExtract(j,Line,@TAB)
      A = Alignment[k]
      W = MaxWidth[k]
      If A=="L" Then Item = StrFix(Item," ",W)
      If A=="R" Then Item = StrFixLeft(Item," ",W)
      If A=="C" Then Item = StrFix(StrFix(" "," ",Int((W - StrCharCount(Item)) / 2)):Item," ",W)
      NewLine = NewLine:Item:"  "
   Next
   NewLine = StrSub(StrTrim("|":NewLine),2,-1)
   NewText = NewText:NewLine:@CRLF
Next
Return NewText
#EndFunction

; OK, let's test it...

TabText = FileGet("before.txt") ; Read tab separated data from file

FixedText = Tab2Fixed(TabText,"LLLLLL") ; All left aligned
GoSub TestDialog

FixedText = Tab2Fixed(TabText,"RRRRRR") ; All right aligned
GoSub TestDialog

FixedText = Tab2Fixed(TabText,"CCCCCC") ; All center aligned
GoSub TestDialog

FixedText = Tab2Fixed(TabText,"RLLCLR") ; Mixed alignment
GoSub TestDialog

Exit

:TestDialog
MyDialogFormat=`WWWDLGED,6.1`

MyDialogCaption=`Test`
MyDialogX=9999
MyDialogY=9999
MyDialogWidth=590
MyDialogHeight=285
MyDialogNumControls=003
MyDialogProcedure=`DEFAULT`
MyDialogFont=`DEFAULT`
MyDialogTextColor=`DEFAULT`
MyDialogBackground=`DEFAULT,DEFAULT`
MyDialogConfig=0

MyDialog001=`219,265,036,012,PUSHBUTTON,DEFAULT,"OK",1,1,32,DEFAULT,DEFAULT,DEFAULT`
MyDialog002=`303,265,036,012,PUSHBUTTON,DEFAULT,"Cancel",0,2,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
MyDialog003=`001,001,586,256,MULTILINEBOX,FixedText,DEFAULT,DEFAULT,3,DEFAULT,"Courier New|6144|40|49","0|0|0",DEFAULT`

ButtonPushed=Dialog("MyDialog")

Return

Article ID:   W17469
File Created: 2008:04:10:15:11:14
Last Updated: 2008:04:10:15:11:14