Link to home
Start Free TrialLog in
Avatar of lindapat
lindapat

asked on

parse file

I have a file that looks like ISA*00**00*27*00523*ZZ*M4788
Could you give me example code on how to parse this file that has * seperating elements?





Avatar of vinnyd79
vinnyd79

Private Sub Command1_Click()
Dim ff As Integer, Ln As String, arrLn() As String
ff = FreeFile
Open "C:\Myfile.txt" For Input As #ff
Do Until EOF(ff)
Line Input #ff, Ln
' split line into array by "*"
arrLn = Split(Ln, "*")

' loop through array
Dim i As Integer
For i = 0 To UBound(arrLn)
    If arrLn(i) <> "" Then MsgBox arrLn(i)
Next i

DoEvents
Loop
Close #ff

End Sub
TheFile = your file

'==code==
Dim tmp() as string

tmp() = split(TheFile, "*")

For a = 0 to ubound(TheFile)
   Debug.Print tmp(a)
Next
Avatar of lindapat

ASKER

I am new at VB.net so please excuse me.  Will this code work? If not would you correct it for me? I want to show each element in label I have created on a form.
Private Sub read835()
        Dim isaLine As String
        Dim elementSep, compositeSep, segmentSep As String
        Dim x As Integer
        Dim tmp() As String
        isaLine = "ISA*00*          *00*          *27*00523          *ZZ*M4788          *040414*2351*U*00401*410500028*0*P*:~"
        elementSep = isaLine.Substring(103, 1)

        For x = 0 To isaLine.Length
            tmp = Split(isaLine, "*")

        Next
        lblMsg.Text = tmp(x)
ASKER CERTIFIED SOLUTION
Avatar of Allpax
Allpax

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
yes but then there will be blank lines.. for instance..

if you split it by * it will look like this:

tmp(0) = "ISA"
tmp(1) = "00"
tmp(2) = "          "
tmp(3) = "00"
tmp(4) = "          "
tmp(5) = "27"
tmp(6) = "00523"
tmp(7) = "ZZ"

etc...

so which lines do you want to get out??  if you do it like i had it, u could get all the information out and check if it's all spaces, and if it is skip it.. such as:


Dim tmp() as string

tmp() = split(TheFile, "*")

For a = 0 to ubound(TheFile)
   if Trim(tmp(a)) <> "" Then MsgBox tmp(a)
Next

ubound fwould be the uppermost index in the array, and trim replaces spaces on the ends of the string, so trim("       ") would be the same as ""