Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

vb6 open file , and use parts of the file 2

Status
Not open for further replies.

tjbradford

Technical User
Dec 14, 2007
229
GB
Hi All,

I have created a vb6 app that reads a file into a textbox, but I would like to only display bits of the file , i'm unsure how to go about this ,

i was thinking of first breaking it into new lines at every "," then grabbing the lines from this file as required.
this will still leave something like the following:
, length: 239 bytes:
Cisco IOS Software, C850 Software (C850-ADVSECURITYK9-M)

I then want to remove ", length: 239 bytes:" from the begining.

I have no idea how to go about this

can anyone help ?

below is a sample of the file i'm trying to read into vb6



***************************************************
20:49:17.453125 CDPv2, ttl: 180s, checksum: 692 (unverified), length 343
Device-ID (0x01), length: 18 bytes: 'rtr.yourdomain.com'
Version String (0x05), length: 239 bytes:
Cisco IOS Software, C850 Software (C850-ADVSECURITYK9-M), Version 12.4(6)T5, RELEASE SOFTWARE (fc1)
Technical Support: Copyright (c) 1986-2006 by Cisco Systems, Inc.
Compiled Sat 07-Oct-06 01:03 by kellythw
Platform (0x06), length: 10 bytes: 'Cisco 857W'
Address (0x02), length: 13 bytes: IPv4 (1) 10.10.10.1
Port-ID (0x03), length: 13 bytes: 'FastEthernet0'
Capability (0x04), length: 4 bytes: (0x0000002b): Router, Transparent Bridge, L2 Switch, IGMP snooping
Prefixes (0x07), length: 5 bytes: IPv4 Prefixes (1): 10.10.10.0/24
VTP Management Domain (0x09), length: 0 byte: ''
 
If the file is structured exactly as you show it here then this will work. May not be the best but....

You will need to make a reference to microsoft scription runtime.

Code:
Private Sub Command1_Click()
Dim f As Scripting.FileSystemObject
Dim ts As TextStream
Dim strLine As String
Dim aryLine()   As String

Dim strFile As String

Set f = New FileSystemObject

With f
Set ts = .OpenTextFile("C:\Test.txt", ForReading)
While Not ts.AtEndOfStream
    strLine = ts.ReadLine
    If InStr(1, strLine, "Cisco IOS") > 0 Then
    '    Debug.Print strLine
       aryLine = Split(strLine, ",", 3)
       Text1.Text = Trim(aryLine(0)) & "," & Trim(aryLine(1))
        Exit Sub
    End If

Wend
End With

Set ts = Nothing
Set f = Nothing
End Sub
 
Oh, I think I'd go with a regular expression ... something like the following will work on a line by line basis:
Code:
[blue]Public Function CleanLine(strLine) As String   
    With CreateObject("vbscript.regexp")
        .Pattern = "(.*), length: \d* bytes:\s*(.*)"
        CleanLine = .Replace(strLine, "$1$2")
    End With
End Function[/blue]

or, if you want to clean the whole lot in one go:
Code:
[blue]Public Function CleanInput(strInputAll) As String
    With CreateObject("vbscript.regexp")
        ' reinstate next two lines if you want to eliminate first line from text
        '.Pattern = ".*CDPv2.*"
        'strInputAll = .Replace(strInputAll, "")
        .MultiLine = True
        .Global = True
        .Pattern = ".*, length: \d* byte.*:\s(.*)"
        CleanInput = .Replace(strInputAll, "$1")
    End With
End Function[/blue]

And, if your source text as given is in a multiline textbox called Text1 and you have another multiline textbox called text2, you might call this like this to test it:

Text2.Text = CleanInput(Text1.Text)


 
strongm (MIS) I'm unable to get your examples working
it error's highlighting CleanInput = 'function call on lefthand side of assignment must return'

New Postjadams0173 (TechnicalUser) your example works and obtains the cisco ios as per the vb6 , how do i reterive more than one line ?
 
Well, all I can say, I'm afraid, is that - as long as you have not modified the function in any way - you are using VB wrong ... the only way I know that you can get that error message from this code is if you are using:

CleanInput = <something>

outside of the function call. In other words you are trying to assign a value to a function, which isn't allowed (except within the function itself, when what you are doing is supplying the return value of the function)
 
I agree with strongm. His code works just fine. Take a form with 2 text boxes and on command button. In the form put the code in the click event. Make sure you have the mulitline set to true on the text boxes.

Code:
Private Sub Command1_Click()
Dim f As Scripting.FileSystemObject
Dim ts As TextStream
Dim strLine As String
Dim aryLine()   As String

Dim strFile As String

Set f = New FileSystemObject

With f
Set ts = .OpenTextFile("C:\Test.txt", ForReading)
While Not ts.AtEndOfStream
    Text1.Text = ts.ReadAll
'    If InStr(1, strLine, "Cisco IOS") > 0 Then
    '    Debug.Print strLine
'       aryLine = Split(strLine, ",", 3)
         Text2.Text = CleanInput(Text1.Text) 'Trim(aryLine(0)) & "," & Trim(aryLine(1))
'        Exit Sub
'    End If

Wend
End With

Set ts = Nothing
Set f = Nothing
End Sub

In a module put strongm's code for reading all the file
Code:
Public Function CleanInput(strInputAll) As String
    With CreateObject("vbscript.regexp")
        ' reinstate next two lines if you want to eliminate first line from text
        '.Pattern = ".*CDPv2.*"
        'strInputAll = .Replace(strInputAll, "")
        .MultiLine = True
        .Global = True
        .Pattern = ".*, length: \d* byte.*:\s(.*)"
        CleanInput = .Replace(strInputAll, "$1")
    End With
End Function
 
Arr , I have got it now, I Have to add the microsoft Scripting references for this to work, Thanks for your patients with me on this, I'm a networker not a coder. :eek:)
 
>I Have to add the microsoft Scripting references

Er, actually, you shouldn't have to. I wrote the code to avoid that ... still, glad you seem to have got things working
 
am I able todo a loop on the module Pattern that can produce the following filtering out the other items within the file.


'rtr.yourdomain.com'
Version 12.4(6)T5, RELEASE SOFTWARE (fc1)
'Cisco 857W'
IPv4 (1) 10.10.10.1
'FastEthernet0'

 
Just add the following function
Code:
[blue]Public Function CloseEnough(strInput As String) As String
    Dim strArray() As String
    strArray = Split(strInput, vbCrLf)
    CloseEnough = strArray(1) & vbCrLf & Trim$(strArray(3)) & vbCrLf & strArray(7) & vbCrLf & strArray(8) & vbCrLf & strArray(9)
End Function[/blue]
to my previously advised CleanInput function, and then

[tt]Text2.Text = CloseEnough(CleanInput(Text1.Text))[/tt]

should get you close enough to the result you want
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top