'TallToWide.vbs 'Converts textfiles with one line per field into ordinary tab-delimited text. 'Call from command line with ' cscript talltowide.vbs 'to see usage instructions 'John Nurick 2003 'USE AT YOUR OWN RISK - ALWAYS MAKE BACKUPS FIRST Option Explicit Dim fso 'FileSystemObject Dim fIn 'TextStream Dim fOut 'TextStream Dim strLine 'output line Dim lngFields 'fields per record, Dim lngBlanks 'blank lines between each record Dim lngJ 'loop counter Dim lngK 'blank line counter Dim SEP 'field separator SEP = Chr(9) Set fso = CreateObject("Scripting.FileSystemObject") 'Check and process command line arguments With WScript If .Arguments.Count < 3 Or .Arguments.Count > 4 Then DisplaySyntaxMessage ElseIf Not IsNumeric(.Arguments(2)) Then DisplaySyntaxMessage Else lngFields = Clng(.Arguments(2)) If .Arguments.Count = 4 Then lngBlanks = Clng(.Arguments(3)) Else lngBlanks = 1 End If wscript.echo "Fields: " & lngFields & " Blanks: " & lngBlanks 'Do the work ProcessFile End If End With 'End of main script Sub ProcessFile() 'Open, read and process the file On Error Resume Next Set fIn = fso.OpenTextFile(WScript.Arguments(0)) If IsEmpty(fIn) Then WScript.Echo "Sorry couldn't open input file " & WScript.Arguments(0) Exit Sub End If Set fOut = fso.CreateTextFile(WScript.Arguments(1)) If IsEmpty(fOut) Then WScript.Echo "Sorry, couldn't open output file" & WScript.Arguments(1) Exit Sub End If On Error Goto 0 Do Until fIn.AtEndOfStream lngJ = 0 strLine = "" Do Until (lngJ = lngFields) Or fIn.AtEndOfStream 'Accumulate fields into line strLine = strLine & fIn.ReadLine & SEP lngJ = lngJ + 1 Loop 'Skip blank lines between records For lngK = 1 to lngBlanks If FIn.AtEndOfStream Then Exit For fIn.ReadLine Next 'Dispose of superfluous separator at end of record If Right(strLine, Len(SEP)) = SEP Then strLine = Left(strLine, Len(strLine) - Len(SEP)) End If fOut.Write strLine & Chr(13) & Chr(10) Loop lngJ = fOut.Line - 1 fIn.Close fOut.Close WScript.Echo "Conversion completed. " & lngJ & " records processed." End Sub Sub DisplaySyntaxMessage With WScript .Echo .ScriptFullName .Echo " Converts textfiles with one line per field into ordinary tab-delimited text." .Echo " Syntax: " .Echo " cscript " & .ScriptName & " InputFileSpec OutputFileSpec Fields [Blanks]" .Echo .Echo " Fields: number of fields per record (excluding blank line(s) between records" .Echo " Blanks: number of blank lines between records. Default is 1." End With End Sub