The magazine of the Melbourne PC User Group
Ready, SET, Go
Tom Coleman
|
 |
In a recent article on the use of the FOR command I mentioned the use of variables. In that case I was discussing %X or %%X which is more or less the same thing.
I said that the FOR command would use anything that DOS would see as a list and step through every item on that list. You could make up any list you like and store it in a variable.
Another use for variables is as a poor man's batch file. More about that soon but first, what are variables and how are they used.
The programmers in our midst know all about variables, string variables and numeric ones too. This time I want to consider the variables used by DOS.
Variables in DOS are called environment variables, to intimidate the novices. They are just ordinary variables and DOS stores them along with a few special variables of its own. Programmers got there before English speaking people and named things with their own voodoo-speak. This accounts for the strange name even though there is nothing strange about it.
Maybe I am getting ahead of myself or some readers. I will explain just what a variable is.
If I said that A=2, and B=4 then A+B would equal 6. That is no big deal. A and B are variables.
I could have said AAA=2 and BBB=4 and it would have been as easily understood.
It follows from that that I could use any combination of letters. So instead of AAA I could use CAT or DOG. The fact that that particular combination of letters makes a word is only a coincidence. It could have been G9L or KJT. They are just letters. However we can use these word making combinations of letters as an aid to telling us what this or that variable is holding.
Thus DATE is a good variable to hold a date except that DOS has reserved DATE for its own purposes so we need to modify it slightly. How about DTE or DAY. Similarly we could have SUBD to hold a subdirectory and TOTAL to hold a number. Anything your imagination can wasp. NAME could hold a name and BIN could hold "rubbish".
There is a rotten trick you can pull where the variable YES holds the word "No" and the variable NO holds flue word "Yes" but more about that some other time.
So how do you set up a variable under DOS. Simple. Suppose you want to set up the variable FLEA to hold the word "Itch". Don't forget that the letters F-L-E-A are only a coincidence; they might just as well he 2:2.71. All you have to do is
SET FLEA=itch
Using some earlier examples
SET BIN=Rubbish
or
SET DAY=Wednesday
A word of caution about choice of variables. DOS reserves some words for its own purposes. Words such as PRN, COMSPEC and the internal commands like DATE, COPY, DEL, VER, and so on. Steer clear of these.
Also avoid using anything that could get confused with an existing filename or DOS command unless that's what you want.
Clearly
SET ABC=Format C:
is asking for trouble.
On the other hand
SET ZZ=CD\games\cards\Blakjak
can save you quite a bit of typing.
Let's look at a few more uses of variables to see how they are used.
First up: DOS needs a way to distinguish between the file BANG.COM and the variable BANG. This is done by wrapping the variable in % marks. Thus: %BANG%. This tends to reduce the incidence of catastrophic typos. Here is one that works from the
command line
SET bang = dir a:
Now enter %BANG%. It works providing you have a disk in drive A:
If we were to
SET FILE= *.BAK;*.TEMP;*.REC;*.CHK
We could then
For gf in (%FILE%) do del %f
which would automate some housekeeping.
Now here comes an interesting bit It is common practice to start application programs with a batch file which is in a subdirectory that is in the PATH (another DOS variable).
Some batch files perform highly complex wonders but many are just a few simple lines. For example a batch file called XTP.BAT might
be
That 15 character batch file, occupies 2 kB or more of your disk space.
If you
SET XTP=C:\XTPRO\XTPRO
you would use at least 19 bytes of RAM. As another copy of the environment is created
for every TSR then this could soon blow out to around 100 bytes.
It is up to you to decide which is more useful: 100 bytes of RAM or 2 kB of hard disk.
Either way you can use environment variables as an alternative to those short batch files.
The batch file would execute with XTP the variable with %XTP%.
There are ways to economise or. the environment by setting these variables after most of the
TSRs have been loaded. By the same taken you can economise on batch files by placing
several different commands in the one file and having a means of choosing. Sounds like I have the subject for my next article. DOS does not allocate very much space to store the environment, just 160 bytes to start with. DOS will automatically increase the size of the environment until a TSR is loaded. After that it requires a command to increase the size. Almost all Autoexec.Bat files load at least one TSR so we can think of the environment as being 160 bytes unless we change it ourselves.
This means that if you are going to get carried away loading all kinds of variables you will have to increase the size. The command to do that is put into the CONFIG.SYS file so you have got to plan ahead.
Shell=C:\dos\command.com /p /e:640
This would give you a 640 byte environment space. (DOS 3.1 requires that the number is a unit in multiples of 16 bytes. to meant 16o bytes, 50 meant 800 bytes. 640 bytes would be 40.)
There are lots of other things to know about variables and this discussion misses a lot more than it hits. It is aimed at your curiosity.
How close did it come?
Give it a go. Go on.
Reprinted from the August 1991 issue of PC Update, the magazine of Melbourne PC User Group, Australia
|