?
Solved

Textstream

Posted on 2003-03-09
8
Medium Priority
?
802 Views
Last Modified: 2008-02-01
Hi!...i have a vb program that writes and reads from a textfile...i'm using the textstream class.i want to read the file line by line...how do i do it without using the 'open...for input' and the AtEndOfLine property?is there any other way to do this?i want to use a timer that reads each line every 5 seconds  so i dont think do until AtEndOfLine will be helpful for me...

Please help...i would really appreciate it!!!!
0
Comment
Question by:liezl
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 15

Expert Comment

by:Ralf Klatt
ID: 8101148
Hi liezl,

Here's a rather complex example for your problem ... I developed it to retrieve the client IP address provided by an ISP from a system text file ... this covers multiple windows versions and multiple languages.
Within the lines you'll see how I use the "textstream":

Public Sub GetClientIP()
    Dim ws, fso, TmpFile, ThisLine, IP, ts, f, theStrg, theStrg2, theSplitter, theSplitter2, theSplitter3, i As Long, j As Long
    Dim a(theNetworkIndex) As Long
    FindLanguage
    numOfAdaps = 0
    Set ws = CreateObject("WScript.Shell")
    Set fso = CreateObject("Scripting.FileSystemObject")
    TmpFile = fso.GetSpecialFolder(2) & "/ip.txt"
    If ws.Environment("SYSTEM")("OS") = "" Then
        SupportedOS = False
        ws.run "winipcfg /all /batch " & TmpFile, 0, True
    Else
        SupportedOS = True
        ws.run "%comspec% /c ipconfig /all >" & TmpFile, 0, True
    End If
    DoEvents
    Set f = fso.GetFile(TmpFile)
    Set ts = f.OpenAsTextStream
    theStrg = ts.readall
    Select Case LanguageInfo.theLanguageID
        Case 3081, 4105, 2057, 1033
            theSplitter = Split(Trim(theStrg), "Host Name")
            'If ws.Environment("SYSTEM")("OS") = "" Then
            '    theSplitter2 = Split(Trim(theSplitter(1)), "DNS-Server")
            'Else
                theSplitter2 = Split(Trim(theSplitter(1)), "Primary")
            'End If
            theSplitter3 = Split(Trim(theSplitter2(0)), ":")
            theStrg2 = Split(Trim(theSplitter3(1)), Chr(13))
            theHostName = theStrg2(0)
            For i = 1 To Len(theStrg) - Len("Description")
                If Mid(theStrg, i, Len("Description")) = "Description" Then
                    numOfAdaps = numOfAdaps + 1
                    a(numOfAdaps - 1) = i + Len("Description")
                End If
            Next
            theSplitter = Split(theStrg, "Description")
            For i = 0 To numOfAdaps - 1
            '    If ws.Environment("SYSTEM")("OS") = "" Then
            '        theSplitter2 = Split(Trim(theSplitter(i + 1)), "Physische")
            '    Else
                    theSplitter2 = Split(Trim(theSplitter(i + 1)), "Physical")
            '    End If
                theSplitter3 = Split(Trim(theSplitter2(0)), ":")
                theStrg2 = Split(Trim(theSplitter3(1)), Chr(13))
                myNetworkInfo(i).theDescription = theStrg2(0)
            Next
            'If ws.Environment("SYSTEM")("OS") = "" Then
            '    theSplitter = Split(theStrg, "Physische Adresse")
            'Else
                theSplitter = Split(theStrg, "Physical Address")
            'End If
            For i = 0 To numOfAdaps - 1
                theSplitter2 = Split(Trim(theSplitter(i + 1)), "DHCP Enabled")
                theSplitter3 = Split(Trim(theSplitter2(0)), ":")
                theStrg2 = Split(Trim(theSplitter3(1)), Chr(13))
                myNetworkInfo(i).thePhysicalAddress = theStrg2(0)
            Next
            theSplitter = Split(theStrg, "IP Address")
            For i = 0 To numOfAdaps - 1
            '    If ws.Environment("SYSTEM")("OS") = "" Then
            '        theSplitter2 = Split(Trim(theSplitter(i + 1)), "Subnet Mask")
            '    Else
                    theSplitter2 = Split(Trim(theSplitter(i + 1)), "Subnet Mask")
            '    End If
                theSplitter3 = Split(Trim(theSplitter2(0)), ":")
                theStrg2 = Split(Trim(theSplitter3(1)), Chr(13))
                myNetworkInfo(i).theIPAddress = theStrg2(0)
            Next
        Case 1031, 3079, 2055
            theSplitter = Split(Trim(theStrg), "Hostname")
            If ws.Environment("SYSTEM")("OS") = "" Then
                theSplitter2 = Split(Trim(theSplitter(1)), "DNS-Server")
            Else
                theSplitter2 = Split(Trim(theSplitter(1)), "Primdres")
            End If
            theSplitter3 = Split(Trim(theSplitter2(0)), ":")
            theStrg2 = Split(Trim(theSplitter3(1)), Chr(13))
            theHostName = theStrg2(0)
            For i = 1 To Len(theStrg) - Len("Beschreibung")
                If Mid(theStrg, i, Len("Beschreibung")) = "Beschreibung" Then
                    numOfAdaps = numOfAdaps + 1
                    a(numOfAdaps - 1) = i + Len("Beschreibung")
                End If
            Next
            theSplitter = Split(theStrg, "Beschreibung")
            For i = 0 To numOfAdaps - 1
                If ws.Environment("SYSTEM")("OS") = "" Then
                    theSplitter2 = Split(Trim(theSplitter(i + 1)), "Physische")
                Else
                    theSplitter2 = Split(Trim(theSplitter(i + 1)), "Physikalische")
                End If
                theSplitter3 = Split(Trim(theSplitter2(0)), ":")
                theStrg2 = Split(Trim(theSplitter3(1)), Chr(13))
                myNetworkInfo(i).theDescription = theStrg2(0)
            Next
            If ws.Environment("SYSTEM")("OS") = "" Then
                theSplitter = Split(theStrg, "Physische Adresse")
            Else
                theSplitter = Split(theStrg, "Physikalische Adresse")
            End If
            For i = 0 To numOfAdaps - 1
                theSplitter2 = Split(Trim(theSplitter(i + 1)), "DHCP aktiviert")
                theSplitter3 = Split(Trim(theSplitter2(0)), ":")
                theStrg2 = Split(Trim(theSplitter3(1)), Chr(13))
                myNetworkInfo(i).thePhysicalAddress = theStrg2(0)
            Next
            theSplitter = Split(theStrg, "IP-Adresse")
            For i = 0 To numOfAdaps - 1
                If ws.Environment("SYSTEM")("OS") = "" Then
                    theSplitter2 = Split(Trim(theSplitter(i + 1)), "Subnet Mask")
                Else
                    theSplitter2 = Split(Trim(theSplitter(i + 1)), "Subnetzmaske")
                End If
                theSplitter3 = Split(Trim(theSplitter2(0)), ":")
                theStrg2 = Split(Trim(theSplitter3(1)), Chr(13))
                myNetworkInfo(i).theIPAddress = theStrg2(0)
            Next
        Case Else
    End Select
    ts.Close
    fso.GetFile(TmpFile).Delete
    Set ts = Nothing
    Set fso = Nothing
    Set ws = Nothing
End Sub


Best regards, Raisor
0
 
LVL 3

Expert Comment

by:KBerger
ID: 8101442
Hi liezl!

Have a look at this:

Create a form, put a timer on it and create a reference to the scripting runtime.

At the beginning of the form-code...

Private m_fsoFileSystem As FileSystemObject
Private m_tsInput As Textstream
Private m_strCurrentLine As String

Private Sub Form_Load()
 Set m_fsoFileSystem = New FileSystemObject
 Set m_tsInput = m_fsoFileSystem.OpenTextFile(<yourfilename>)
 Me.Timer.Enabled=True
End Sub

Private Sub Timer_Timer()
 If Not m_tsInput.AtEndOfStream
  m_strCurrentLine = m_tsInput.ReadLine
 Else
  Timer.Enabled=False
  MsgBox "End of file reached!"
 End If
End Sub

Hope that helps!!

Kristof
0
 

Author Comment

by:liezl
ID: 8101585
hi kberger!

thanx for your code...one more thing...is it possible to access one textfile for reading and writing at the same time?that's what i've been trying to do.i did two separate programs that access the same file at the same time and it wont work...thanx! :)


0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 11

Expert Comment

by:rdrunner
ID: 8101782
Well its "kinda" possible...


You can access the file forReading anf forAppending...

ForAppending allows to add more lines to a textfile while forreading will just read. If you try to use forWriting then the file will be overwritten and you cant open it for reading anymore...

(does this make sense???)

Hope this helps

0
 

Author Comment

by:liezl
ID: 8149690
hi kristof...the previous code u gave me reads the same line which is the first line.it wont go to the next line.
0
 
LVL 15

Expert Comment

by:Ralf Klatt
ID: 8149988
Hi liezl,

I make it short this time:

   Set ws = CreateObject("WScript.Shell")
   Set fso = CreateObject("Scripting.FileSystemObject")
   TmpFile = fso.GetSpecialFolder(2) & "file.txt"
   Set f = fso.GetFile(TmpFile)
   Set ts = f.OpenAsTextStream
   theStrg = ts.readall
   ...

At this point you have the whole textstream in the "theStrg" variable. You can now perform any action with all of the text.

Tabs [chr(13)] or line breaks [chr(10)] can be seperated with the "split" command.
You can check before how many line breaks there are with running a counter like:

Dim i as long, j as long
For i = 1 to len(theStrg)
   If mid(theStrg, 1, i) = chr(10) Then j = j + 1
Next

"i" holds the number of lines you're having.

Now you can strip each line:

Dim theSplitter as Variant
theSplitter = Split(theStrg, chr(10))
For i = 1 to j
   ...do what ever with... theSplitter(i)
Next

You can even split up each line if there are any fields in a line ...

Hope this time its easier to understand!

Best regards, Raisor
0
 
LVL 15

Accepted Solution

by:
Ralf Klatt earned 375 total points
ID: 8150001
Sorry, I forgot about the timer: If you put the upper code in to a timer function and set the timer to 5000 ... well, the action will be performed every 5 seconds ...
0
 

Author Comment

by:liezl
ID: 8150289
thanks!the program is already working!
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…
Suggested Courses
Course of the Month15 days, 8 hours left to enroll

743 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