• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2422
  • Last Modified:

VBscript, Sort Data, text file

I have a list of data in a text file I want to sort in a specific format.
From this:
0101080030
0101080031
0101080032
0101080033
0101080034
0101080035
0101080040
0101080041
0101080042
0101080043
0101080044
0101080045
0101080050
0101080051
0101080052

To this:
0101080010,0101080011,0101080012,0101080013,0101080014,0101080015
0101080020,0101080021,0101080022,0101080023,0101080024,0101080025

The idea is to read each line, if the number ends in a '0' start a new line to a new text file in comma delimited format until you get another number that ends in a '0' then start a new line.

Any assistance appreciated.
0
TekNoxious
Asked:
TekNoxious
  • 3
  • 3
  • 2
1 Solution
 
Michel PlungjanIT ExpertCommented:
Hmm. In JS it would be
str.replace(/\n/g,',').replace(/0,/g,'0\n')
0
 
sirbountyCommented:
This should do it..

Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
arrData = Split(objFSO.OpenTextFile("C:\Source.txt").ReadAll, vbNewLine)
Set objOutput = objFSO.CreateTextFile("C:\NewOutput.txt") 
x = 0
For Each strData in arrData
  x = x +1
  If x 6 7 Then
    objOutput.Write strData & ","
  Else 
    x = 0
    objOutput.WriteLine  strData
  End
Next 
objOutput.Close

Open in new window

0
 
Michel PlungjanIT ExpertCommented:
what does

  If x 6 7 Then


mean???

Where is the test for the last digit being 0?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
TekNoxiousAuthor Commented:
I am with mplungjan about that line and missing function.
I would accept Jscript if that would make this an easier task. I am welcome to learning something new.
0
 
sirbountyCommented:
Sorry - my rushed attempt before heading out the door...
This should work for you:
Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objFile : Set objFile = objFSO.OpenTextFile("F:\Test.txt")
Dim objOutput : Set objOutput = objFSO.CreateTextFile("F:\New.txt")
 
Do While Not objFile.AtEndOfStream
  If strTemp = "" Then strTemp = objFile.ReadLine
    If Right(strTemp, 1) = "0" Then
      WriteLine strData
       strData = ""
      strData = strData & strTemp & ","
    Else
      strData = strData & strTemp & "," 
    End If
    strTemp = ""
Loop
 
WriteLine strData
 
objoutput.Close
 
Sub WriteLine (strData)
  If strData = "" Then Exit Sub
  End If
  objOutput.WriteLine Left(strData, Len(strData) -1)
End Sub

Open in new window

0
 
TekNoxiousAuthor Commented:
The 'End If' needed to be removed from the Sub then it worked!
Thank you for the VBscript lesson.
0
 
sirbountyCommented:
accidental delete...originally
  If strData = "" Then
    Exit Sub
  End If

0
 
TekNoxiousAuthor Commented:
Thank you for your time and support.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now