Results 1 to 11 of 11

Thread: Read text file and display in listview report style

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Feb 2000
    Posts
    17

    Post

    I run a command netstat -a > text.txt
    and get the result as below:
    Active Connections

    Proto Local Address Foreign Address State
    TCP p2:1030 P2:0 LISTENING
    TCP p2:80 P2:0 LISTENING
    TCP p2:135 P2:0 LISTENING
    TCP p2:6543 P2:0 LISTENING
    TCP p2:443 P2:0 LISTENING
    TCP p2:1032 P2:0 LISTENING
    TCP p2:1032 APIITNT:nbsession ESTABLISHED
    TCP p2:137 P2:0 LISTENING
    TCP p2:138 P2:0 LISTENING
    TCP p2:nbsession P2:0 LISTENING
    TCP p2:1028 P2:0 LISTENING
    TCP p2:1029 P2:0 LISTENING
    TCP p2:1031 P2:0 LISTENING
    UDP p2:6543 *:*
    UDP p2:nbname *:*
    UDP p2:nbdatagram *:*

    Now I want write a porgram to read the test.txt and
    display in a listview with four column, which is same with above.
    How can I display the result?
    Can anyone help me???
    thanks

  2. #2
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744

    Post

    Sure thing.

    Code:
    Private Sub Command1_Click()
        Dim xColumnHeader As ColumnHeader
        Dim xListItem As ListItem
        Dim intFFN As Integer
        Dim strPath As String
        Dim strBuffer As String
        Dim arrString() As String
        Dim i As Integer
        
        
        With ListView1
            .View = lvwReport
            Set xColumnHeader = .ColumnHeaders.Add(, , "Protocol")
            Set xColumnHeader = .ColumnHeaders.Add(, , "Local Address")
            Set xColumnHeader = .ColumnHeaders.Add(, , "Foreign Address")
            Set xColumnHeader = .ColumnHeaders.Add(, , "State")
        End With
        
        strPath = "d:\Text.txt"
        intFFN = FreeFile
        Open strPath For Input As intFFN
        Do Until EOF(intFFN)
            Line Input #1, strBuffer
            arrString = Split(strBuffer, " ")
            With ListView1
                Set xListItem = .ListItems.Add(, , arrString(0))
                For i = 1 To UBound(arrString)
                    xListItem.SubItems(i) = arrString(i)
                Next
            End With
        Loop
        Close #intFFN
    End Sub
    ------------------

    Serge

    Senior Programmer Analyst
    [email protected]
    [email protected]
    ICQ#: 51055819

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Feb 2000
    Posts
    17

    Post

    The source code was got compile error
    in the line

    arrString = Splits(strBuffer, " ")

    with the error message

    Sub or Function not defined

    What happen with it?
    I am using visual Basic 5.


  4. #4
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744

    Post

    Sorry about that. Split funtion is available in VB6 only. So here's the working code (with custom SplitArray function):

    Code:
    Public Function SplitArray(pString As String, pDelimeter As String) As Variant
        Dim iPos As Integer
        Dim arrTemp()
        Dim i As Integer
        Dim strTemp As String
        
        strTemp = pString
        
        iPos = InStr(strTemp, pDelimeter)
        Do Until strTemp = ""
            ReDim Preserve arrTemp(i)
            If InStr(strTemp, pDelimeter) Then
                arrTemp(i) = Trim(Left(strTemp, InStr(strTemp, pDelimeter) - 1))
                strTemp = Mid(strTemp, InStr(strTemp, pDelimeter) + 1)
            Else
                arrTemp(i) = Trim(strTemp)
                strTemp = ""
            End If
            i = i + 1
        Loop
        SplitArray = arrTemp
    End Function
    
    Private Sub Command1_Click()
        Dim xColumnHeader As ColumnHeader
        Dim xListItem As ListItem
        Dim intFFN As Integer
        Dim strPath As String
        Dim strBuffer As String
        Dim arrString As Variant
        Dim i As Integer
        
        
        With ListView1
            .View = lvwReport
            Set xColumnHeader = .ColumnHeaders.Add(, , "Protocol")
            Set xColumnHeader = .ColumnHeaders.Add(, , "Local Address")
            Set xColumnHeader = .ColumnHeaders.Add(, , "Foreign Address")
            Set xColumnHeader = .ColumnHeaders.Add(, , "State")
        End With
        
        strPath = "d:\Text.txt"
        intFFN = FreeFile
        Open strPath For Input As intFFN
        Do Until EOF(intFFN)
            Line Input #1, strBuffer
            arrString = SplitArray(strBuffer, " ")
            With ListView1
                Set xListItem = .ListItems.Add(, , arrString(0))
                For i = 1 To UBound(arrString)
                    xListItem.SubItems(i) = arrString(i)
                Next
            End With
        Loop
        Close #intFFN
    End Sub
    ------------------

    Serge

    Senior Programmer Analyst
    [email protected]
    [email protected]
    ICQ#: 51055819

  5. #5
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744

    Post

    Let's try a little bit different approach:

    Code:
    Public Function SplitArray(pString As String, pDelimeter As String, pArr() As String) As Boolean
        Dim iPos As Integer
        Dim arrTemp() As String
        Dim i As Integer
        Dim strTemp As String
        Dim iTempUbound As Integer
        
        On Error Resume Next
        
        strTemp = pString
        
        iPos = InStr(strTemp, pDelimeter)
        Do Until strTemp = ""
            ReDim Preserve arrTemp(i)
            If InStr(strTemp, pDelimeter) Then
                arrTemp(i) = Trim(Left(strTemp, InStr(strTemp, pDelimeter) - 1))
                strTemp = Mid(strTemp, InStr(strTemp, pDelimeter) + 1)
            Else
                arrTemp(i) = Trim(strTemp)
                strTemp = ""
            End If
            i = i + 1
        Loop
        
        iTempUbound = UBound(arrTemp)
        If Err.Number = 0 Then
            pArr = arrTemp
            SplitArray = True
        End If
    End Function
    
    Private Sub Command1_Click()
        Dim xColumnHeader As ColumnHeader
        Dim xListItem As ListItem
        Dim intFFN As Integer
        Dim strPath As String
        Dim strBuffer As String
        Dim arrString() As String
        Dim i As Integer
        
        
        With ListView1
            .View = lvwReport
            Set xColumnHeader = .ColumnHeaders.Add(, , "Protocol")
            Set xColumnHeader = .ColumnHeaders.Add(, , "Local Address")
            Set xColumnHeader = .ColumnHeaders.Add(, , "Foreign Address")
            Set xColumnHeader = .ColumnHeaders.Add(, , "State")
        End With
        
        strPath = "d:\Text.txt"
        intFFN = FreeFile
        Open strPath For Input As intFFN
        Do Until EOF(intFFN)
            Line Input #1, strBuffer
            If SplitArray(strBuffer, " ", arrString) = True Then
                With ListView1
                    Set xListItem = .ListItems.Add(, , arrString(0))
                    For i = 1 To UBound(arrString)
                        xListItem.SubItems(i) = arrString(i)
                    Next
                End With
            End If
        Loop
        Close #intFFN
    End Sub

    Although, the first solution is working fine on my machine.

    ------------------

    Serge

    Senior Programmer Analyst
    [email protected]
    [email protected]
    ICQ#: 51055819

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Feb 2000
    Posts
    17

    Post

    The source code was got compile error again
    in the line
    arrString = SplitArray(strBuffer, " ")

    with the error message

    Can't assign to array

    What happen with it?


  7. #7
    Guest

    Post

    Guess you forgot to change
    Dim arrString() As String
    to
    Dim arrString As Variant



    ------------------

    Vincent van den Braken
    EMail: [email protected]
    ICQ: 15440110
    Homepage: http://www.azzmodan.demon.nl




  8. #8

    Thread Starter
    Junior Member
    Join Date
    Feb 2000
    Posts
    17

    Post

    There is an error in the line

    If SplitArray(strBuffer, " ", arrString) = True Then

    with the message

    compile error
    type mismatch:array or user-defined type expected

    ps : For Serge,
    thanks you because wasting your time for my module.

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Feb 2000
    Posts
    17

    Post

    I try that before
    when i change it into

    Dim arrString As Variant

    a runtime error '9' occur

    with message

    subscript out of range

    what it mean then???

  10. #10
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744

    Post

    How about this. Give me your Email address and I'll send you this code in a module. So you can run exactly what I run.

    ------------------

    Serge

    Senior Programmer Analyst
    [email protected]
    [email protected]
    ICQ#: 51055819

  11. #11
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744

    Post

    Even though I sent you an email with the solution, I still want to post it here so whoever else want it to use it.
    VB5 cannot assign to array like this: Array1 = Array2 if those arrays are not variant data type. So here is the working SplitArray function that was giving the problem.

    Code:
    Private Function SplitArray(pString As String, pDelimeter As String, pArr() As String) As Boolean
        Dim iPos As Integer
        Dim arrTemp() As String
        Dim i As Integer
        Dim strTemp As String
        Dim iTempUbound As Integer
        
        On Error Resume Next
        
        strTemp = pString
        
        iPos = InStr(strTemp, pDelimeter)
        Do Until strTemp = ""
            ReDim Preserve arrTemp(i)
            If InStr(strTemp, pDelimeter) Then
                arrTemp(i) = Trim(Left(strTemp, InStr(strTemp, pDelimeter) - 1))
                strTemp = Mid(strTemp, InStr(strTemp, pDelimeter) + 1)
            Else
                arrTemp(i) = Trim(strTemp)
                strTemp = ""
            End If
            i = i + 1
        Loop
        
        iTempUbound = UBound(arrTemp)
        If Err.Number = 0 Then
            ReDim pArr(UBound(arrTemp))
            For i = LBound(arrTemp) To UBound(arrTemp)
                pArr(i) = arrTemp(i)
            Next
            SplitArray = True
        End If
    End Function

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width