Click to See Complete Forum and Search --> : Split Problems with Inconsistent Text File


VBNovice
June 16th, 2001, 12:22 PM
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

shree
June 16th, 2001, 07:19 PM
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.

VBNovice
June 16th, 2001, 09:42 PM
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

shree
June 16th, 2001, 10:35 PM
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().

VBNovice
June 16th, 2001, 10:58 PM
Ahhh! Looks Good..Much appreciated!

-Dave

Cimperiali
June 18th, 2001, 04:36 AM
...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.