September 25, 2009

Essential Environment Variable Control with Set

This nearly 30-year-old command has only gotten better with age
RSS
Subscribe to Windows IT Pro | See More Utilities Articles Here | Reprints | Or get the Monthly Online Pass—only $5.95 a month!

One of the most basic batch-file commands is Set—a circa-1982 command that displays, sets, or removes environment variables. Set has only gotten better with age, and in a way that most Set users are completely unaware of. So, this month, I'd like to offer some help to all of you batch-file writers with a new look at Set—specifically, its abilities to generate random numbers and perform arithmetic.

A Little Background
Environment variables store installation-specific information such as what a machine's name is (i.e., computername), whether the system is a 32-bit or 64-bit OS (i.e., processor_architecture), and where the OS should look for executable programs (i.e., path). Environment variables tend to fall into three categories: those the OS depends upon (e.g., the three I just cited), those that third-party applications use (e.g., I have QuickTime on my system, and it creates an environment variable named QTJAVA), and those that Windows' batch files use.

No matter how simple a programming language is—and Windows' batch language is among the simplest—that language needs a place for programmers to tuck away in-process data. Environment variables fill that role. However, a tool is necessary to get data into environment variables so that they're useful, and that tool is the Set command.

I know what you're thinking. Everyone understands the Set command! Come on! But do you really? Did you know that it can do arithmetic, or that it can perform substring operations on values in existing environment variables? Did you know that it can solicit input, or that it can generate random numbers? It can do all of those things—and more—but for now we'll focus on two of those capabilities, which reveal a couple interesting Set options.

Generate Random Numbers
Consider the simple batch file called guessnumber.cmd that Listing 1 shows. Copy this text to Notepad, save it to some folder on your path, and give the file the name guessnumber.cmd. Now, open a command prompt, type guessnumber, press Enter, and follow the prompts. It's a simple guessing game in which the batch file picks a random number, prompts you for a guess, then tells you that your guess is correct, too high, or too low—and if you haven't nailed it yet, it offers you another chance. (You should be able to guess the number in 16 or fewer guesses.) It's not the most amazing game in history, but it does run on Windows Server 2008 Server Core.

Consider this line, which picks the random number to guess:

set gamevalue=%random%


The key piece of this code is %random%, a function (built into Windows' command processor since Windows 2000 Server) that generates a random number between 0 and 32,767.

Once it generates the random number, the game needs to be able to repetitively ask the user for guesses. Prior to Win2K, batch files didn't offer a useful way to solicit input (as anyone who's ever wrestled with the Choice command knows). Ever since Win2K, though, Set has had a useful input capability via its /p option, as you see in this statement:

set /p latestguess=What do you think it is? 


This command looks like

set /p environmentvariablename=[prompttext]


You can see that the prompt is, "What do you think it is?" Note the space after the question mark; it makes for nicer formatting. Whatever you type goes into an environment variable named latestguess.

Perform Arithmetic
I mentioned before that, given a range of values (32,768 possible integers), anyone should be able to guess any value within 16 tries. Let's modify that game, restricting the user to no more than 16 tries. We'll need a bit of arithmetic to keep track of how many guesses he or she has made, and you can see the result in Listing 2 in a revised version of guessnumber. A quick look at the guessnumber2.cmd code—which I've written to be short rather than elegant (hey, I'm no rock-star developer) —shows that the two batch files are different only in a few lines that introduce a new environment variable named guessesleft. I introduce that variable in a statement that anyone who's worked with any Microsoft OS since DOS 2.0 will recognize:

set guessesleft=16


Set's new arithmetic abilities appear in this statement:

set /a guessesleft=%guessesleft%-1


The trick is the /a option, which lets you do not only addition, subtraction, multiplication, division, and modulo computations, but also a few bitwise logical operations.

I hope I've given you a couple reasons to revisit an old and trusted—but perhaps underutilized—friend in Set. Join me next month for a look at an enhanced version of Set called Setx!
   Previous  [1]  2  3  Next 


Windows IT Pro Community
Blogs





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

Getting ready for Windows 7, eLearning series with Mark Minasi

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


SQL Server Magazine Office & SharePoint Pro DevProConnections asp.netPRO ITTV
IT Library Technology Resource Directory Connected Home Windows SuperSite
 
 Windows IT Pro is a Division of Penton Media Inc.
 © 2009 Penton Media, Inc.     Terms of Use | Privacy Statement | Reprints and Licensing