Link to home
Start Free TrialLog in
Avatar of Jammerules
Jammerules

asked on

How to read a text file line by line that contains multiple lines in visual basic

Hello fellas, here is the deal:

I have an input text file like this:

"Please enter user name followed by an equal to operator and further followed by Records Management contact name"
AAAAA=BBBBB
CCCC=DDDDD
EEEEE=FFFFF

And my VB code to read the input file is like this:
    Open "C:\file.txt" For Input As #1
    Do Until EOF(1)
    Line Input #2, sUserRecMgtCnt
    iIndex1 = InStr(Trim(sUserRecMgtCnt), "=")
    sUserInput = Mid(Trim(sUserRecMgtCnt), 1, iIndex1 - 1)

I am able to get the first part of the line (until the'=' operator) but I dont understand how I could capture the word past the '=' operator. I tried using the Chr$(13) (and Chr$(10)) for capturing the carriage return and/or new line character, but totally in vain.
So, I would like to know how to capture the second part of the string from '='  operator. Also, the second question is -

While reading the above input file, I have a standard comment in the beginning "Please enter user name followed by...da.da...daaa.daa...daaaa..daaa"

I want this line be ignored while reading  the input. How could I do that?

Thanks for any help offered
Jammer


           
ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg 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
SOLUTION
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
SOLUTION
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
You need to use the SAME file handle number for you operations:

    Open "C:\file.txt" For Input As #1
    Do Until EOF(1)
    Line Input #2, sUserRecMgtCnt

You open it as #1 and use "1" in the loop, but are reading from #2?
good catch, idle_mind !

you should indeed not "hard-code" the file number, but use freefile:


 dim fHandle as integer  
 fHandle = freefile
 Open "C:\file.txt" For Input As #fHandle
 while not EOF(fHandle)
    Line Input #fHandle, sUserRecMgtCnt

    if sUserRecMgtCnt like "*=*" then
      iIndex1 = InStr(Trim(sUserRecMgtCnt), "=")
      sUserInput = Mid(Trim(sUserRecMgtCnt), 1, iIndex1 - 1)
      sValue = Mid(Trim(sUserRecMgtCnt), iIndex1 + 1)
    end if

  wend

 close #fHandle
I personally would use the FileSystemObject to read past however many lines your comment header is [one then?].  Usually people have some sort of leading delimiter for such things, e.g., like VB has for its own comment type - you use " it seems.

If the file really does look like this ...

"Please enter user name followed by an equal to operator and further followed by Records Management contact name"
AAAAA=BBBBB
CCCC=DDDDD
EEEEE=FFFFF


    Dim fso As New Scripting.FileSystemObject
   
    Dim txs As TextStream
   
    Dim sa() As String
   
    Dim n As Integer
   
    Set txs = fso.OpenTextFile(fileName, ForReading)
   
    txs.SkipLine
   
    sa = Split(Replace(txs.ReadAll, vbCrLf, "="), "=")
   
    For n = 1 To UBound(sa) - 1 Step 2
   
        Debug.Print sa(n)
   
    Next n


Output:

BBBBB
DDDDD
FFFFF


>>Dim fso As New Scripting.FileSystemObject

Add a reference to 'Microsoft Scripting Runtime' for that, or use:

Dim fso As Object

Set fso = CreateObject("Scripting.FileSystemObject")
@peetm:
just a small remark: your suggestion to replace vbcrlf by =, and the read only every second array item has a potential "flaw":
if some day, a value containing a "=" will be stored, this will break.
altough this is not the case here, this could be "forgotten" when copying the code over to another app... and boum...
>>@peetm:
just a small remark:

Yes, any delimited source that doesn't adhere to a bnf strict grammar will have similar problems.  One imagines here that '=' appears only in this context.
Avatar of LostIt6
LostIt6

You seem to have a parsing problem. I think you ned the Mid function built into VB to get what comes after the =. I wrote up a quick example.

To try this code now create a new VB Exe. Add a textbox to the form, make set multiline True and Scrollbars Vertical.

' EXAMPLE
Private Sub Form_Load()
    Dim FF As Long
    Dim mFName As String
    Dim mLine As String
    Dim mKey As String
    Dim mValue As String
    Dim mPos As Long
    mFName = "c:\file.txt"
    FF = FreeFile
    Open mFName For Input As #FF
        Do While Not EOF(FF)
            Input #FF, mLine
            ' Make sure the line contains a key value pair
            mPos = InStr(mLine, "=")
            If mPos > 0 Then
                ' The key is to the left of the =
                mKey = Left(mLine, mPos - 1)
                ' The value is to the right of the =
                mValue = Mid(mLine, mPos + 1)
                ' #######
                ' Add the rest of your code the processes the key and value here
                ' #######
                ' The next three lines just display the pairs in a textbox
                Text1.SelStart = Len(Text1.Text)
                Text1.SelText = mKey & " - " & mValue & vbCrLf
                Text1.SelStart = Len(Text1.Text)
            End If
        Loop
    Close #FF
End Sub
I apoligize, you are using mid... but my Example solution is cound.
I wouldn't use Trim inside of Instr. It can lead to errors in the return value of Instr. Trim your string before you process it.

' Your code modified
    FF = FreeFile
    Open "C:\file.txt" For Input As #FF
    Do Until EOF(FF)
    Input #FF, sUserRecMgtCnt
    sUserRecMgtCnt = Trim(sUserRecMgtCnt)
    iIndex1 = InStr(sUserRecMgtCnt, "=")
    sUserInput = Mid(sUserRecMgtCnt, 1, iIndex1 - 1)
    sValue = Mid(sUserRecMgtCnt, iIndex1 + 1)
Avatar of Jammerules

ASKER

Hello everybody.

I am extremely sorry for not responding until now. I had been busy with my other stuff and did not have time to test your solutions. Please give me one more day before I post a reply and accept one/multiple solutions. Thanks a bunch again for all the replies. I appreciate each and everybody's time!

Jammer
Thanks each and everyone for theri respective answers. Although, I could not award points to everyone with their just fantastic answers, I had to share points between the first three persons who answered as the question arrived!

Y'all are wonderful...keep up the good work!