Windows IT Pro is the leading independent community for IT professionals deploying Microsoft Windows server and client applications and technologies.
  
  
  Advanced Search 


October 13, 2009

Using Setx to Parse a Text File

A little tinkering provides a solution that beats the scripting route
RSS
Subscribe to Windows IT Pro | See More Utilities Articles Here | Reprints | Or get the Monthly Online Pass—only $5.95 a month!

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 "Enhanced Environment Variable Control with Setx" (InstantDoc ID 102706), 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 70.165.73.5:)(2,3 bytes=32)(2,4 time=30ms)(2,5 TTL=113)


This shows that in the phrase Reply from 70.165.73.5: bytes=32 time=30ms TTL=113, Setx found six "words": Reply, from, 70.165.73.5:, 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


It responds

 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 70.165.73.5:)(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.

End of Article



Reader Comments

You must be a registered user or online subscriber to comment on this article. Please log on before posting a comment. Are you a new visitor? Register now




Top Viewed ArticlesView all articles
WinInfo Short Takes: Week of November 23, 2009

An often irreverent look at some of the week's other news, including some post-PDC some soul searching, a Google Chrome OS announcement and a Microsoft response, Windows 7 off to a supposedly strong start, the Jonas Brothers and Xbox 360, and so much more ...

Command Prompt Tricks

One reader shares his tip for setting up the command prompt to reflect a remote path. ...

2009 Windows IT Pro Editors' Best and Community Choice Awards

Picking a favorite product from an impressive crowd of competitive offerings is never an easy task, and such was the case with our Editors' Best and Community Choice awards this year. ...


Related Events Deep Dive into Windows Server 2008 R2 presented by John Savill

Configuration Manager SP1 and R2 Overview

Check out our list of Free Email Newsletters!

Windows OSs eBooks Understanding and Leveraging Code Signing Technologies

A Guide to Windows Certification and Public Keys

SQL Server Administration for Oracle DBAs

Related Windows OSs Resources Introducing Left-Brain.com, the online IT bookstore
Looking for books, CDs, toolkits, eBooks? Prime your mind at Left-Brain.com

Discover Windows IT Pro eLearning Series!
Clear & detailed technical information and helpful how-to's, all in our trademark no-nonsense format


Windows IT Pro Home Register FAQ for Windows WinInfo News
Europe Edition About Us Contact Us/Customer Service Media Kit Affiliates / Licensing  
SQL Server Magazine Office & SharePoint Pro DevProConnections IT Job Hound
Left-Brain.com Technology Resource Directory asp.netPRO ITTV Windows SuperSite 
 
 Windows IT Pro is a Division of Penton Media Inc.
 © 2009 Penton Media, Inc. Terms of Use | Privacy Statement