Subscribe to Windows IT Pro

 

Get Newsletters

  • Get the Latest News
  • Product Updates
  • Helpful Tricks
  • Productivity Tips

Subscribe Now!

September 25, 2009 12:00 AM

Essential Environment Variable Control with Set

This nearly 30-year-old command has only gotten better with age
Windows IT Pro
InstantDoc ID #102597
Rating: (2)

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!

Related Content:

ARTICLE TOOLS

Comments
  • Robert
    2 years ago
    Jan 15, 2010

    The 5th line in guessnumber2.cmd should be removed. It reveals the answer!

  • Kermit
    3 years ago
    Sep 30, 2009

    wow, no comments Mark. That's a shame. I had no idea that SET had evolved like this & I know I will find it useful. Thanks for the article.

You must log on before posting a comment.

Are you a new visitor? Register Here

advertisement

advertisement

White Papers

Get your Windows 7 deployment off to the right start by implementing PC lockdown. A locked-down environment is easier and cheaper to support since users are less likely to make unnecessary changes to the core system configuration - read more here!

Essential Guides

Is your iSCSI "lossy"? The reality is that most off-the-shelf Ethernet hardware deployed for iSCSI can lose packets, resulting in slow performance or application downtime. Learn how to assess your current iSCSI infrastructure and engineer an advanced iSCSI SAN infrastructure.

Web Seminars

What's the best way to keep your network safe from malware? In this web seminar, security expert Greg Shields suggests an alternative method to the traditional blacklisting approach that is common with anti-virus and anti-malware solutions.

eLearning Series

We bring the experts direct to you to share their real-world perspective and expertise. During each event, three sessions stream in real time, so you can learn, ask questions, and get solutions.
Upcoming event: Getting the Most with Exchange 2010 with Paul Robichaux

Subscribe to Windows IT Pro!

Windows is a trademark of the Microsoft group of companies. Windows IT Pro is used by Penton Media Inc. under license from owner.