This nearly 30-year-old command has only gotten better with age
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 /p latestguess=What do you think it is?
set /p environmentvariablename=\[prompttext\]
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 /a guessesleft=%guessesleft%-1