DOS Batch in Plain English
Home News & Articles Tutorials Downloads About this site

Variables, Loops and What-ifs

What is a variable?  A variable is where it all happens.  Without variables, our programs would be very dry & similiar.  You can modify variables.  You can call upon variables when you really need someone (...or at least when you need a piece of data).  Type 'set' in DOS.  Notice the list.  Now type 'set foo=bar'.  You just set the variable FOO equal to BAR.  Type 'set' again to see your evil doings.  What is that good for you say?  Everthing.  Type 'echo %FOO%'.  Notice that it echos the value of the variable FOO which we set to BAR.  Command.com evaluates the letters inbetween the percentage signs and replaces the variable with the contents of the memory space named "FOO".  It's bait and switch man, bait and switch.

Now comes the bad news.  You can't do math in DOS batch.  You can add in NT batch, but that's not the title of this tutorial.  Try 'set num=1' & then 'set num=%num%+1'.  It should be two you say?  Ha!  1+1=1+1 in DOS batch.  Type 'echo %num%' to find that you have never seen such a thing since that Taoist monk in math class.

If only we could if.  In DOS type: 'if 1==1 echo yeppers'.  Since "1" is exactly the same text as "1", Command.com executes "echo yeppers" which we know will just spit out "yeppers".  If we use 'if 1==1.0 echo yeppers', it just won't work.  Why the double equal sign?  Type 'if /?'.  Because Big Brother says so.

Before we hit the loops, we need to arm you with something.  Ctrl+C.  If you hold Ctrl and hit C, it will break the loop.  Ctrl+Break will do the same thing.  Use them well.  Otherwise, you'll be burning countless CPU cycles wondering "Why do I feel like I've been here before?".  Let's go into Notepad and do a File>New to start a new horror.  Type:

@ECHO OFF

REM Das Loop V1.0

SET MAX=..............................

REM Labels start with a colon.

:MOREDOTS

SET DOTS=%DOTS%.

ECHO %DOTS%

IF %DOTS%==%MAX% GOTO NOMOREDOTS

GOTO MOREDOTS

REM Command.com will jump to this label if %DOTS% equals %MAX% that we set earlier

:NOMOREDOTS

ECHO.

ECHO I've really had enough dots, thanks.

ECHO.

Breakdown:  SET DOTS=%DOTS%. adds one period to the end of the variable dots, which is blank at first.  It is now one dot.  The next pass comes around, replaces %DOTS% with one period and adds a period on the end.  This goes on until %DOTS% becomes equal to %MAX% when we jump to the label called NOMOREDOTS.

Save this beaut as C:\WINDOWS\DESKTOP\DOTS.BAT  Notice the caps in dots.bat?  It sometimes is easier since Command.com is not case sensitive.  It's good practice to use caps in everywhere but in the user prompts and comments.  BECAUSE WHO WANTS TO READ THIS ALL THE TIME?

Now try running dots.  Runs great.  A neat little loop.  Try running it again.  Your first very own software bug!  Now you know what it's like to be an easy target like Microsoft.  Type 'set' to see that the variable DOTS never reset itself.  It's still in memory so it kept adding to itself.  To fix this little oversight, put a 'SET DOTS=' above the 'SET MAX=..............................'  Doing a SET (Variable)= clears out the variable from memory.

Variables will reset themselves once you exit that instance of our friend, Command.com.  Try setting some variables and exiting Command.com by typing 'Exit'.  If you run Command.com again, the variable won't be set.

So how do you set a variable to always be set?  You can modify Autoexec.bat.  With your newfound batching skills, you'll make short work of it.

previous

next

Page 3

Page 1 - Introduction

Page 2 - The beginning

Page 3 - Variables, Loops and What-ifs

Page 4 - Pipes, Redirection and Switches

Page 5 - Where to go with batch

hide

A variable is a named memory space that is capable of holding something.  In DOS, all set variables can be viewed by typing set at the DOS prompt.

hide

Ok.  So maybe that's pushing it.  Variables really don't care about your needs and wants.  You better just get a date.