Textstream

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!!!!
liezlAsked:
Who is Participating?
 
Ralf KlattConnect With a Mentor Principal ConsultantCommented:
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
 
Ralf KlattPrincipal ConsultantCommented:
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
 
KBergerCommented:
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
liezlAuthor Commented:
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
 
rdrunnerCommented:
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
 
liezlAuthor Commented:
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
 
Ralf KlattPrincipal ConsultantCommented:
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
 
liezlAuthor Commented:
thanks!the program is already working!
0
All Courses

From novice to tech pro — start learning today.