Subscribe to Windows IT Pro
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)
Listing 1: Guessnumber.cmd

 echo off
set gamevalue=%random%
echo I'm thinking of a number between 0 and 32767.
:looptop
set /p latestguess=What do you think it is?
set guessright=false
if /i %latestguess% equ %gamevalue% echo You got it! & set guessright=true
if /i %latestguess% lss %gamevalue% echo Too small.
if /i %latestguess% gtr %gamevalue% echo Too large.
if NOT %guessright%

true goto looptop

^^^^ ^^^^Listing 2: Guessnumber2.cmd

echo off
set guessesleft=16
set gamevalue=%random%
echo I'm thinking of a number between 0 and 32767.
echo %gamevalue%
:looptop
set /p latestguess=What's your guess (only %guessesleft% left)?
set guessright=false
set /a guessesleft=%guessesleft%-1

if /i %latestguess% equ %gamevalue% echo You got it! & set guessright=true
if /i %latestguess% lss %gamevalue% echo Too small.
if /i %latestguess% gtr %gamevalue% echo Too large.
if /i %guessesleft% leq 0 goto byebye
if NOT %guessright%true goto looptop
:byebye
if NOT %guessright%==true echo Sorry ... you used up all your turns. The number was %gamevalue%.


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

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