Solved

Reading text file

Posted on 2004-09-28
13
439 Views
Last Modified: 2010-05-02
This is my file

{4:
:20:LM083119ZUDD1  
:21:OB04.02097      
:32A:040617USD1962,94
:50:XXXXXXXX XXXXXXXXX XXXXXXX
CROESELANN 18
P O  BOX 17100
3500 HG UTRECHT, THE NETHERLANDS
:53D:/000000005003
XXXXXXXX XXXXXXXXX XXXXXXX
CROESELANN 18
P O  BOX 17100
3500 HG UTRECHT, THE NETHERLANDS
:58D:/000000005258
XXXXXXXX XXXXXXXXX XXXX XXXX
TWO EXCHANGE SQUARE 39TH FLOOR
8 CONNAUGHT PLACE
CENTRAL HONG KONG
:90C:002
:61:0406170615D X1962,94XXXX0000000000005003   00000
:61:0406170615C X1962,94XXXX0000000000005258   00000
-}$

I need to read this file  as
condition #1 >> if the first 3 characters are :50 then put that record to the first element to an array
condition #2 >> Read the following records until a line begining with : is encountered and store it in next element

so condition #1 >> would match :50:XXXXXXXX XXXXXXXXX XXXXXXX so it would be myarray(1)= "condition #1 "

condition #2 >> would match
CROESELANN 18
P O  BOX 17100
3500 HG UTRECHT, THE NETHERLANDS
so it would be myarray(2)= "condition #2 "


How do I do this ?
0
Comment
Question by:jitganguly
  • 6
  • 4
  • 2
  • +1
13 Comments
 
LVL 8

Expert Comment

by:bramsquad
Comment Utility
this should do the trick

Open "C:\your_file.txt" For Input As #1

Dim myLine As String
Dim arr() As String
Dim count As Integer

count = 1

While Not EOF(1)
    'get a new line
    Line Input #1, myLine

    If Mid$(myLine, 1, 3) Like ":50" Then
        arr(count) = "condition #1 "
        count = count + 1

        'get the rest of the values
        While ((Mid$(myLine, 1, 1) <> ":")  Or (Not EOF(1)))
            Line Input #1, myLine
        Wend

        arr(count) = "condition #2 "
        count = count + 1
    End If

Wend
0
 
LVL 4

Expert Comment

by:Rick_Townsend
Comment Utility
> While ((Mid$(myLine, 1, 1) <> ":")  Or (Not EOF(1)))

That should be   AND (Not EOF(1)))
shouldn't it?

Also, it's simple and much safer to get the file number from the FreeFile function.

eg:
inFileNo = FreeFile
Open "C:\your_file.txt" For Input As inFileNo
...
Line Input #inFileNo, myLine
0
 
LVL 4

Expert Comment

by:Rick_Townsend
Comment Utility
Plus, close your files:

Close inFileNo
0
 
LVL 20

Author Comment

by:jitganguly
Comment Utility
Let me test and will get back soon
0
 
LVL 8

Expert Comment

by:bramsquad
Comment Utility
not sure about the freefile function

but i think it should be OR and not AND....

im saying "get the lines until you come across a colon, OR until you come across the end of the file"

if you had an AND there, it might be an infinite loop, becuase the file may not have a colon at the end of the file.

~b
0
 
LVL 20

Author Comment

by:jitganguly
Comment Utility
Sorry does not work

Open strNextFile For Input As #2
        While Not EOF(2)
           
            Line Input #2, strNextLine
            If Mid$(strNextLine, 1, 3) Like ":50" Then
                strParty(1) = strNextLine
                i = i + 1
             MsgBox strNextLine
                While ((Mid$(strNextLine, 1, 1) <> ":") Or (Not EOF(2)))
                    Line Input #2, strNextLine
                Wend
                i = i + 1
                MsgBox strNextLine
            End If
        Wend


The first  strNextLine should produce me :50:XXXXXXXX XXXXXXXXX XXXXXXX and second one
CROESELANN 18
P O  BOX 17100
3500 HG UTRECHT, THE NETHERLANDS
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 8

Expert Comment

by:bramsquad
Comment Utility
what do you want to hold in your array?

the information?  if so, strNextLine will not get that whole address becuase it is on three different lines, so it will get it one at a time.

how do you want to store this information?

0
 
LVL 20

Author Comment

by:jitganguly
Comment Utility
Lets forget about array for the time being

Here is my code
 Open strNextFile For Input As #2
        While Not EOF(2)
           
            Line Input #2, strNextLine
            If Mid$(strNextLine, 1, 3) Like ":50" Then
                strParty(1) = strNextLine
                i = i + 1
            MsgBox strNextLine
              While ((Mid$(strNextLine, 1, 1) <> ":") Or (Not EOF(2)))
              'If (EOF(2) Or Mid$(strNextLine, 1, 1) = ":") Then Exit Do
                     Line Input #2, strNextLine
                    jit = jit & strNextLine
              Wend
                i = i + 1
                MsgBox jit
            End If
        Wend

MsgBox strNextLine should be ":50:XXXXXXXX XXXXXXXXX XXXXXXX "

MsgBox jit should be

"CROESELANN 18
P O  BOX 17100
3500 HG UTRECHT, THE NETHERLANDS
"

0
 
LVL 100

Expert Comment

by:mlmcc
Comment Utility
Should be an AND

>> im saying "get the lines until you come across a colon, OR until you come across the end of the file
However you are testing
    While ((Mid$(myLine, 1, 1) <> ":")  Or (Not EOF(1)))

A  OR   B  is equivalent to ~A AND ~B

mlmcc
0
 
LVL 20

Author Comment

by:jitganguly
Comment Utility
>>Should be an AND
Coming out from the loop for the very first read

While ((Mid$(strNextLine, 1, 1) <> ":") And (Not EOF(2)))


0
 
LVL 8

Accepted Solution

by:
bramsquad earned 250 total points
Comment Utility
my bad, i was wrong...

either way, here is a working program which will output what you are looking for

Private Sub btnStep5_Click()
        Open "D:\test.txt" For Input As #2
        Dim strNextLine As String
        Dim strParty(1) As String
        Dim test As String
        Dim i As Integer
       
        While Not EOF(2)
           
            Line Input #2, strNextLine
            If Mid$(strNextLine, 1, 3) Like ":50" Then
                strParty(i) = strNextLine
                i = i + 1
             MsgBox strNextLine
             test = ""
             Line Input #2, strNextLine
                While ((Mid$(strNextLine, 1, 1) <> ":") And (Not EOF(2)))
                    test = test + strNextLine + vbCrLf
                    Line Input #2, strNextLine
                Wend
                i = i + 1
                MsgBox test
            End If
        Wend
        Close #2
End Sub
0
 
LVL 20

Author Comment

by:jitganguly
Comment Utility
Looks like it is working, I need to get it verified by my users will let you know and award you 'A' tomorrow

Thanks
0
 
LVL 20

Author Comment

by:jitganguly
Comment Utility
It worked. Thanks
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now