CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jun 2001
    Posts
    3

    Split Problems with Inconsistent Text File

    I am running a program that shells out to DOS, runs Netstat -an then dumps it to a text file. I want to take this data and put it into a ListView with 5 categories. Most of that I have down. It is parsing the text file that I am having trouble with because the spacing and format is inconsistent:

    Active Connections

    Proto Local Address Foreign Address State
    TCP 0.0.0.0:135 0.0.0.0:0 LISTENING
    TCP xx.xxx.xxx.xxx:137 0.0.0.0:0 LISTENING
    TCP xx.xxx.xxx.119:138 0.0.0.0:0 LISTENING
    TCP xx.xxx.xxx.xxx:139 0.0.0.0:0 LISTENING
    TCP 127.0.0.1:1025 0.0.0.0:0 LISTENING
    TCP 127.0.0.1:2412 0.0.0.0:0 LISTENING
    TCP 127.0.0.1:110 0.0.0.0:0 LISTENING
    TCP xxx.xxx.xxx

    Mainly the problems comes from the amount of characters in any IP address. They are not all the same so I have had a heck of a time using Split function to work poperly. Any ideas or suggestions on another angle of attack for this would be greatly appreciated!

    -Dave


  2. #2
    Join Date
    Mar 1999
    Location
    Nepal
    Posts
    540

    Re: Split Problems with Inconsistent Text File

    From the dat you give, I see that it is quite consintent.
    You have a string "TCP" Space LocalIP Space ForeignIP "LISTENING", don't you?
    So just use the Split function with " " (One space) as the delimeter.
    Any number of characters in the IP address doesn't make any difference.


  3. #3
    Join Date
    Jun 2001
    Posts
    3

    Re: Split Problems with Inconsistent Text File

    Actually, it formatted rather nicely when I posted it here ;-)

    In the file there maybe 1 space between TCP and Local IP and then x number of spaces between the next, etc. This is where the inconsistency is. Sorry I wasn't more clear.

    Dave


  4. #4
    Join Date
    Mar 1999
    Location
    Nepal
    Posts
    540

    Re: Split Problems with Inconsistent Text File

    Then try this


    Strng = "TCP 127.0.0.1:110 0.0.0.0:0 LISTENING"
    Do
    OldLen = len(Strng)
    Strng = Replace(Strng, " ", " ") 'Replace two spaces with one
    Loop While OldLen <> len(Strng) 'Until there is no change
    MsgBox Strng




    It will trim your string so that there is only one space between the fields. Then you can use Split().



  5. #5
    Join Date
    Jun 2001
    Posts
    3

    Re: Split Problems with Inconsistent Text File

    Ahhh! Looks Good..Much appreciated!

    -Dave


  6. #6
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: Out of votes!

    ...And I am already out of votes!!...


    Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, Bruno Paris and all the other wonderful people who made and make Codeguru a great place. Come back soon, you Gurus.
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

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

Featured