Handling Live Web Content in WSH - Part 1 - Processing the Response
(Page 2 of 4 )
We begin processing the html response by setting up an If statement. Basically, we’re just going to make sure that our variable actually holds some data before we try to play with it. If it doesn’t, that means that there was an error connecting to the site.
If strHTMLText <> "" Then
varStart = inStr(1, strHTMLText, "Current IP Address:", _
vbTextCompare) + 19
If varStart Then varStop = inStr(varStart, strHTMLText, "</body>", _
vbTextCompare)
If varStart And varStop Then strIP = mid(strHTMLText, varStart, _
varStop - varStart)
Okay, our first line checks to makes sure that the strHTMLText variable is not empty. If it isn’t, then we begin processing it. The second line uses the VB inStr() function. The inStr() function will match our search parameter within a given string and return an integer indicating the starting character where our search criteria appears within that string. So we look at the source code and pick a unique string that appears before our IP address. In this case we’ve chosen “Current IP Address:”. The inStr() function will return the position of the first character is finds in the matching string, so we add 19 (the number of characters in our search string) to be sure that it returns the value just before our IP address.
The ideal way to do this would be to use our IP address as the search string, but since we don’t know what it is, this is a viable workaround. We could also use regular expressions by using the RegExp object, but that’s a little more difficult, and it also adds quite a bit of extra code.
In the third line we go back to the inStr() function to find the starting character after our IP address. So again, we check the source code and find a unique search string. This time inStr() will return the correct value so we don’t need to adjust it. We’ll assign this to the variable varStop.
Now that we’ve narrowed down the information that we want, our next goal is to remove all of the extra text from our string. We do this in the fourth line by implementing VB’s mid() function to cut one string from the middle of another. Essentially, it performs a left trim and a right trim at the same time using the values that we specify. We assign the results of the mid() function to a string variable strIP. If all went well strIP should contain only our IP address.
Next: A Little Error-Handling >>
More Windows Scripting Articles
More By Nilpo