Link to home
Start Free TrialLog in
Avatar of LD147
LD147Flag for United States of America

asked on

Ading commas to text file

I have code which opens a text file, which contains many rows of data that look like this:

R3aa12345678c000000032700:006215
R3da87563214c000000606400:007615
R3Ae65987412c000000021100:000715
R3ed32165498c000000336000:005515
R3ac98765432c000000617600:004415
R3aa12345678c000000620601:001315
R3da87563214c000000527201:009915

Each row is 32 bytes long, but I need to add a comma after the 2nd, 12th, 20th, 23rd, 28th, and 30th characters, for each row in the text file.  What would be the best solution for this?  The reason for me wanting to add commas is so that I can then save it as a CSV file, and then import it into a Database.
Avatar of Brook Braswell
Brook Braswell
Flag of United States of America image

My suggestion is that as you read each row into your string
              Dim sLine as string
210      Open sFile For Input As InFile
220      While Not EOF(InFile)
230          Line Input #InFile, sLine
240          sline = mid(sline,1,2) & "," & mid(sLine,3,8) & "," & mid(sLine,11,10) & "," & mid(sline,21,3) & "," & mid(sline,24,5) & "," & mid(sline,29,2) & "," & mid(sline,31,2)
250      Wend
260      Close InFile


you could process sLine after it's change and write it out to a different file...


Avatar of LD147

ASKER

Well, here's the code I have so far.  I opened the text file this way (shown in the code).  I didn't read it in line by line.
' open rawdata.txt
        Dim streamtodisplay As StreamReader
        streamtodisplay = New StreamReader("C:\gatewaydata\rawdata.txt")
        txtMain2.Text = streamtodisplay.ReadToEnd
        streamtodisplay.Close()
 
        ' add code for commas here
 
 
        ' show text file, with commas, inside the txtMain2 text box
        txtMain2.Select(0, 0)

Open in new window

For what you want you either add the commas to the Text box or you read the file one line at a time...
Alternatively you could read one line at a time and go ahead and created your .csv then stream that into your text box...




dim sFile as string
dim sOUT as string
dim sLINE as string
dim sText as string
 
sFile = "C:\gatewaydata\rawdata.txt"
' for your output if you choose
sOUT = "C:\Gatewaydata\rawdata.csv"
sText = ""
Open sFile for Input as #1
do while not EOF(1)
   Line Input #1, sLINE
   sline = mid(sline,1,2) & "," & _
           mid(sLine,3,8) & "," & _
           mid(sLine,11,10) & "," & _
           mid(sline,21,3) & "," & _
           mid(sline,24,5) & "," & _
           mid(sline,29,2) & "," & _
           mid(sline,31,2)
   sText = sText & iif(trim(stext) = "","",vbcrlf) & sline 
LOOP
CLOSE #1
txtMain2.Text = sText

Open in new window

Avatar of LD147

ASKER

I used that code, but it's showing these errors (see pic).


vb1.JPG
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of LD147

ASKER

Idle_Mind - perfect!  That works!  Thank you so much.  Also, Brook1966, I appreciate you trying to help, thanks also.