A little tinkering provides a solution that beats the scripting route
I recently needed to create a batch file that would give me an idea of whether my Internet connection was running well, so I thought I'd begin by doing a single ping to a particular DNS server on the Internet, redirecting that output to a text file, extracting the round-trip time in milliseconds, and displaying the results. (I figured the batch file could run in a little text window if I could minimize the output.) But how could I grab just a particular bit of text from an app's output? The Setx command provided the answer.
I introduced Setx—a command in Windows Vista and later (and in several older resource kits)—in "Essential Environment Variable Control with Setx", but I didn't get a chance to show you its ability to parse text out of input. Setx views a text file as a set of text lines, each of which contains words. It then lets you identify the particular word that you want to extract with two coordinates: the line that it's found in and its word position on that line. Setx counts from zero, not one, so the coordinates to tell Setx to grab the fifth word on the third line would be (4,2).
Setx is useful for extracting words out of text files that have regular structures, such as the captured output of a Ping command. (I've also used it to grab data out of Ipconfig outputs.) The command needs a text file to work with, so I'll create one for it by pinging my website once with the command
ping -n 1 www.minasi.com>pr.txt
But how do I tell Setx which line and word number identifies the reported round-trip time in milliseconds? You could start counting, of course, but that would be tedious. Instead, you can type
setx /f pr.txt /x
which causes Setx to dump all the words it can find in the text, along with their coordinates. For example, some of that output looks like
(2,0 Reply)(2,1 from)(2,2 184.108.40.206:)(2,3 bytes=32)(2,4 time=30ms)(2,5 TTL=113)
This shows that in the phrase Reply from 220.127.116.11: bytes=32 time=30ms TTL=113, Setx found six "words": Reply, from, 18.104.22.168:, bytes=32, time=30ms, and TTL=113. The one I want, time=30ms, is prefixed with 2,4, meaning that it's the fourth word in the third line. Now I've got the information I need to tell Setx to take the captured output of a Ping command, extract the round-trip time, and put it into an environment variable that I'll call rtt:
setx rtt /f pr.txt /a 2,4
Extracted value: time=30ms. SUCCESS: Specified value was saved.
That will get time=30ms into the environment variable rtt, and that might be all I need, but what if I don't want the time= part? Well, as you've probably guessed, Setx's idea of a "word" is a set of characters surrounded by the beginning of a line, a space, or the end of a line. (It also uses tabs as word delimiters.) To remove the 30 from time=30ms, I could use Setx's /d option to define extra delimiters (i.e., the equals sign and the letter m):
setx rtt /f pr.txt /d "m","=" /a ...
But that /a option needs coordinates, and I've rearranged the whole coordinate system quite a bit. How do I determine where 30 shows up with these new delimiters? I'd use another /x command, but this time with the delimiters in place:
setx /f pr.txt /d "m","=" /x
Believe me, that command results in a real mess. To thin the output a bit, I can filter out all the lines except for the ones that contain 30, like so:
setx /f pr.txt /x /d "=","m"|findstr "30"
That shows just two lines:
(2,0 Reply)(2,1 fro)(2,2 22.214.171.124:)(2,3 bytes)(2,4 32)(2,5 ti)(2,6 e)(2,7 30)
(2,8 s)(2,9 TTL)(2,10 113)(7,0 Mini)(7,1 u)(7,2 30)(7,3 s)
(7,4 Maxi)(7,5 u)(7,6 30)(7,7 s)(7,8 Average)(7,9 30)(7,10 s)
The 30 I'm looking for is in the first of those two lines—yes, it's a bit uglier to read because Setx has removed the letter m—and so the coordinates I want are (2,7).
Now I have a command that will pull out the roundtrip time:
setx rtt /f pr.txt /d "m","=" /a 2,7
And finally, I get the result
Extracted value: 30. SUCCESS: Specified value was saved.
If you're putting together something that winnows out just a few bits of information, and you don't feel like scripting, give Setx a try. It might save you some time.