(Image: DOS Logo)


Doctor
DOS
Betamax's

DOS
VARIABLES


An Examination of the DOS
Replaceable Characters and Strings

(Image: %_%)

 


********

As one's usage of DOS increases along
with the knowledge that accompanies it, a
desire to have DOS do more will emerge. One
of the first power tools many DOS users implement
is the batch file. A basic DOS Batch File Tutorial at this
website will get you started, but this article is aimed at
adding a very important batch file utensil to your DOS tool box.

    What follows is basic information on DOS variables, what they are, and their typical usage. As with other discussions at this website, deep detail will not be gone into. If you want to know more about this aspect of DOS, see your DOS manual's section on Variables, and/or the Command Reference, and/or the On-Screen Help. Alternatively, obtain some DOS books. (See DOS Publications.)

    Realise that not everything presented here may work for your manufacturer or version of DOS. In particular, 1980s and 90s DOS versions often react differently than today's. As well, there are even variances among versions and software from a given manufacturer. What is presented here will use common, recent methods for the most part. Despite differences, the concepts still hold true, and learning to use variables will be one more brick in the structure of a strong DOS knowledge.




NOTHING IN THIS ARTICLE MAY BE REPRODUCED
WITHOUT PERMISSION FROM THE AUTHOR ©


 % What is a Variable?     A variable is a term to which a numerical value or character string is assigned. It consists of the name and the value or string that particular name represents. (From this point onward in this article, the variable contents will be referred to as its "value".) In DOS, depending on the type of variable used, the name can be one or more letters, numbers or ASCII characters, but with some restrictions. ASCII symbols may include the space characters, but there are times when spaces can cause problems. Other symbols that can cause havoc are the percent sign ( % ), redirection signs ( > < ), the pipe symbol ( | ), and equals ( = ). It is better if they are not used as variable names until one becomes proficient at using variables. However, there are no restrictions on the characters that make up the value of the variable, although with some, the results may differ from the intended purpose.

    There are two related types of variables: Environmental Variables and Replaceable-Character Variables. Both replace one term with another and save the information to memory, but the former's information is kept in a memory area called the "DOS Environment" until it is removed or the computer is rebooted. The latter typically use single characters or numbers as the names, and as soon as the operation is completed, the information is usually deleted. (The DOS Environment will be discussed momentarily.)

    Because the value can change, or vary, these are known as "Variables". They are usually accompanied by percent signs, but there are DOS versions that deviate somewhat. As an example, the method used by 4DOS for its built-in environmental variables is to precede them with " %_" (percent underscore), whereas DR, MS, and PC DOS all use one percent sign to precede and another to succeed an environment name. 4DOS recognises this and also does make usage of it. Other DOSes may differ slightly. Some programs may use a `$' (dollar sign) on one or both sides.

    Replaceable-Character Variables use a single percent sign or a single dollar sign before the name in all the DOS versions I have come across, but that may not be universal. In addition, within batch files the "FOR" command uses two percent signs, one directly after the other. (More on double percent signs later.)


ENVIRONMENTAL VARIABLES

THE ENVIRONMENT

    All variable values are held in memory. Those issued by the "SET" command are placed into the environment, a reserved memory area. The information in there is typically used by DOS and its software for reference as to how DOS or a program should act, or where to locate certain files, or how to configure itself -- or a combination of these. DOS and its software programs consult this area to see if there are instructions or information in there for them. So essentially, the environment contains the conditions and surroundings in which DOS and its programs work. These conditions can be imposed by DOS itself, its software, or by a user such as yourself.


    Each Environmental Variable consists of the Variable Name to be assigned, and the second part of it is the contents of that variable. As already mentioned, these are placed into memory by the DOS "SET" command. This command may be used to view, add, delete or modify environmental variables.

    When setting an environment variable, do not use spaces between the name, the equals sign and the value unless one wants the name and/or the value to include spaces. If letters are used in the name, they are typically stored in uppercase by most versions of DOS, most of the time. (Variances to this depend on one's setup and DOS version used.) As said earlier, the name given will usually be preceded and succeeded by a percent sign ( % ) after it is stored. This might look like:


 SET level=2

    The above will assign the value of `2' to the name "level". The information is stored in the environment as " %LEVEL% " which remains there until it is deleted or the system is rebooted. So anywhere %LEVEL% is found, it will be replaced with the value of `2'. Using such variables means that the user need not type the contents out every time. Instead, the shortcut of %LEVEL% can be employed. The above example may not seem like much of a shortcut because it takes more to type "level" than `2', but as you read on, the advantage shall become apparent.

    A variable is not only a shortcut to fewer keystrokes. If one were to not assign information as a variable and then at some point wanted to change that information, every instance of this change would have to be retyped. If there are dozens of references to that information, it would mean a lot of retyping. Using a variable to represent that information means only one change needs to be made, and that needs to be done only where the variable was originally defined. Once one assigns the variable its new contents, all the other locations where the variable is used will automatically reference that change of contents through the variable's name.

    Note that if you want to change a variable you will not edit it directly. Instead, the equivalent edit may be achieved by restating the name of the variable but with the changed value. So if one issues:


SET NUMBER=111

   
   ...it may be "edited" to a new value by issuing:

SET NUMBER=121

    Another usage is to assign DOS variables to do specific procedures. This places information into memory so that DOS may make use of it to perform a dedicated task -- one typically assigned by the user. Barring a reboot, these variables may be kept in memory indefinitely and used by DOS or its programs as needed during any computer work session. If one wishes to use them in every session, their "SET" statements must be included in a start-up file which is then used to place them into the environment memory area at every boot-up. That file can be AUTOEXEC.bat, or in many versions of DOS, these can be loaded from CONFIG.sys. A separate batch file may also be used, but it must be called from AUTOEXEC.bat. In addition, there are third-party programs that can load and alter the DOS environment.

    Realise that if a second command processor is started by the user as a separate session, that any variables set by the user are not passed on to it unless they were set in the AUTOEXEC.bat or CONFIG.sys. However, when the user returns to the first session by typing "EXIT", variables that had been set will still be there. By the same token, though, any variables placed into the environment in the second session will be lost upon returning to the first. (Note that there are methods which allow variables to be carried to and/or from other environments, but these are beyond the scope of this article.)


DOS INTERNAL VARIABLES

    A few of the environmental variables that come with DOS are shown below. These are set by DOS itself, but they may be specified by the user, as necessary, either through the boot-up configuration file or during the course of any computer work session. The latter may be done at the command line or from within a batch file.


    Realise that not all versions of DOS support all of the above, and that these are not the only ones available. The ones above can take different forms with different DOSes.

    Remember that any changes made to variables that had originally been set during boot-up will return to their defaults after a reboot. If you want to keep any of the changes, you will have to place the SET commands into AUTOEXEC.bat, or preferably into a dedicated "Settings" file which is accessed upon boot-up.


 % Why Might I Want to Use the Environment beyond What was Already Set?
    Well, first of all, as mentioned above, one may wish to change the DOS default settings. Many users prefer a custom prompt beyond "  C:\>  ". As well, users might wish to include other directories in the DOS path. To see what environment variables are already in memory, issue "SET" by itself. A list will appear. Each can be changed by specifying the variable name, an equals sign and a new value. Thus, resetting %PROMPT% by:


 SET PROMPT=$D $P

    ...will show the current Date, a space, and then the current Path (drive & directory) at the command line.


An Aside:     Do not confuse the PROMPT command's usage of dollar signs with the usage of the dollar sign for variables as discussed in this article.
    PROMPT uses the dollar sign to designate what are called "meta characters" in order to set the prompt one sees at the command line. These are similar in execution to, but are not the same as environment variables. (See your DOS manual's PROMPT explanation.)


    Another reason to want to reset some of the values differently from the DOS defaults is in regards to the TEMP directory. This is where temporary work is performed and temporary information is stored -- a scratch pad of sorts. One may wish to locate that directory on a faster RAM drive; this would be done by setting up a RAM drive, creating the desired directory on the drive, and then resetting the %TEMP% variable to point to that directory.

    Users may also want to use the environment to dictate how certain DOS commands will work, or to set up varying configurations. An example of the former would be the %COPYCMD%. This sets the overwrite characteristics of the COPY, MOVE, and XCOPY commands in some DOS versions to make them ask the user before files are overwritten. Conversely, it can allow automatic overwriting. The %CONFIG% variable references a different startup configuration which permits additional choices to be made later on based on its contents.


AUTOMATING DOS WORK

    Beyond those types of assignments, one may want to set variables to automate tasks that DOS will do. These might require a different value for each circumstance, hence the need for a variable term. So this might be seen as:


 :LOOP
 (Commands)

 IF "%CYCLE%" == "20" GOTO END
 GOTO LOOP

 END

    In operation, a batch file could be set up to run a procedure over & over until `n' cycles have passed. Lines could test for say, 20 cycles, and then determine what the file should do next. Above, this snippet of a batch file is testing to see if %CYCLE% has reached the value of `20'. If it hasn't, the batch file will run its procedure again. If it has, the batch operation will end. In usage, the value of %CYCLE% would vary (increase) by one for each time through the procedure as per instructions represented in this example by "(Commands)".

    If you want to remove a variable entirely, issue "SET (name)=". So to delete the %CYCLE% variable in the above example, it would be "SET CYCLE=". Do not include any percent signs when issuing SET commands.

    The opposite side of this technique is if a value that must be kept constant might change in the course of operations. Placing the original value into the environment would preserve it for later usage:


 SET PARAMETER=%1

    This takes the first item typed on the command line after the batch file name and assigns it the name of %PARAMETER%. If the contents of %1 were to change during the course of the procedure, but the first contents were those which are required, one could not use %1 any more. However, since %PARAMETER% remembers those original contents, it may be referenced instead. This brings us to:


REPLACEABLE-CHARACTER VARIABLES

    These are often used in conjunction with environment variables and can have changing values, too. They are always preceded by at least one percent sign ( % ) and some by two. Programs such as DOSKEY and Toddy use the dollar sign ( $ ) as a replaceable-character identifier. All are just ways for the operating system or its programs to recognise variables. Powerful 4DOS can designate up to 255 replaceable-character variables at the command line, but most DOS versions are limited to 9 at a time.

    Here is an example of one used at the command line: Suppose you have a batch file called "CTF.bat". It is used to copy text files to a flashdrive so as to be able to take them home or elsewhere. You don't want it to copy all the files every time but only a certain one. So you set it up so that the batch file's main line reads:


 COPY %1.TXT G:

    The `%1' is a replaceable-character variable. The batch file will fill in whatever you type at the command line and issue the COPY command upon it. Thus, if you enter "CTF READ-ME" at the command line, CTF.bat will copy a file called READ-ME.txt from the current directory to your `G' flashdrive. The next time, you might issue "CTF DIRECTORY" so then the file copied to the flashdrive would be "DIRECTORY.txt".

    Notice that this example uses only one percent sign. Usually, single ones are used for replaceable characters, while double percent signs are used when setting a "FOR" string variable. More on that in a moment...

    Let's use a second variable in the above example. Continuing to use the flashdrive, suppose you have more than one USB port and wish to copy a differing file to a differing drive, your batch file could read:


 COPY %1.TXT %2:

    The first variable would contain the file to be copied, while the second would be the drive to where it is to be copied. Thus, "CTF BACKUP H" would copy "BACKUP.txt" to the `H' drive. %1 was replaced with "BACKUP", while %2 was replaced with `H'. Then, DOS did the copy operation.


The FOR COMMAND

    Sometimes spoken of as "FOR-IN-DO" (a term rarely used today), FOR allows one or more operations to be carried out on a collection of items. These could be drives, files, commands, or more intricate groups of values. A basic example in a batch file might be:


FOR %%F IN (TEST-1.txt TEST-2.txt) DO TYPE %%F

    This means that FOR each file withIN the parentheses DO type (show) its contents on the screen. The variable " %%F " will represent each of the two text files in turn and display its contents. Notice the two percent signs before the variable character of `F'. This is how this command is used in a batch file in most DOS versions. At the command line, this would be reduced to one percent sign. Be aware that the case of the variable must be the same within a given FOR line. I used `F', so the second instance of it must not be `f', in this case.

    Also realise that the `F' character could be any letter. I chose `F' because it represents the Files shown in the brackets. If drive letters were inside the brackets, I would use `%%D', if numbers were in there, I would use `%%N', and so on. Alternatively, some DOS versions might allow full words and other characters to be used as the variable:


FOR %%FILES IN (TEST-1.txt TEST-2.txt) DO TYPE %%FILES

    The FOR command has many additional features depending on your DOS version. The purpose in this article is to keep it simple so one may understand only the aspect of variables. To learn more about this very powerful command, see your DOS documentation.



 % Where May I Use Variables?
    User variables may be employed at the command line and within programs. They are most commonly used with, or within, batch files.


 % Can You Provide More Examples?
    One example would be the current date. Perhaps you want to have a backup file made every day which name incorporates that day's date. You don't want to have to type in that differing date every day and would rather have DOS do this for you.

    Older versions of DOS do not include this variable as set by DOS itself, but there are ways to get the date into the environment. CTULD.bat shown elsewhere at this website explains how that is done. New versions of DOS already set it as " %DATE% " or similar.

    Once the current date is in the environment, one may issue commands such as:


 MD C:\BACKUP\%DATE%
 COPY C:\DATA\*.* C:\BACKUP\%DATE%

    This makes a directory which name will be the current date, and then copies the data files to it. Regardless of what the date is, a directory by that name will be created and files copied to it every day. You understand that the date varies every day; however the variable called " %DATE% " does not. It always remains as %DATE%, or whatever you choose to use. Since I incorporate a lot of date operations in my batch files, I prefer " %CUR-DATE% " to differentiate it from other date variables I employ.


 % How Do I Create a Variable?
    Simply use the "SET" command. The syntax to place a variable in the environment is " SET V=TS ", where `V' is the name you wish to assign as the Variable, and `TS' is the Text String to be assigned.

    Thus, if you place a line like "SET NAME=%1" in a batch file called LOGIN.bat, when Judy wants to log into whatever has been set up for her purposes, she would enter "LOGIN JUDY". Afterwards, the name can be tested, and if a match is found, DOS can do work depending on the name used:


 :: LOGIN.bat
 ::
 @ECHO OFF

 SET NAME=%1

 IF "%NAME%" == "ADAM" GOTO ADAM
 IF "%NAME%" == "JUDY" GOTO JUDY
 IF "%NAME%" == "MAHENDRA" GOTO MAHENDRA

 :ADAM
 (Adam's login procedure)
 GOTO END

 :JUDY
 (JUDY's login procedure)
 GOTO END

 :MAHENDRA
 (MAHENDRA's login procedure)

 :END

    That was a simple example with no error checking or security; nor was there any method presented to deal with a user entering lower or mixed case. However, it gives an idea of how a variable might be used for such a purpose.


 % Tell Me again about those Percent Signs
    Whenever you set a variable, it will show up in the environment as a character string with percent signs, one at each end. This format is used in most DOS versions. 4DOS recognises either a single leading percent, or one leading and one trailing. (Remember that some programs use a dollar sign instead.)

    For replaceable character purposes, the single leading percent sign is used, typically %1 through %9, but remember that 4DOS can recognise up to `%255' ! Exceptions to the single percent include at least the FOR command, and DOSKEY and Toddy variables. The latter two use the dollar sign, so instead of `%1', they will use `$1'. Toddy can also recognise `$10' through `$99'.

    As already mentioned regarding the FOR command, when it's used at the command line, one percent sign is used, while in a batch file, two are needed for most versions of DOS:


 FOR  %F IN (*.TXT *.DOC) DO COPY  %F G:

 FOR %%F IN (*.TXT *.DOC) DO COPY %%F G:

    Either one will copy the items listed inside the parentheses to the `F' drive, but the second line is to be used within a batch file. DOS will eliminate the first percent sign preceding the `F' character in each instance when used in a batch file. As with earlier examples, DOS will replace the variable `%F' (or `%%F') with the specified item(s). In the above example, the items are all the .txt and .doc files in the current directory, and they will be copied to the `G"' flashdrive.

    One other thing I should mention is that for most DOS versions, two consecutive percent signs are used in batch files when one wishes to echo, or to pass, a single percent sign; that is, when a single percent is needed to be seen on screen or in another file:


ECHO %%1

   This will show on screen as "%1", not the designated text as may have
been assigned to "%1". That is, "%1" will not be replaced in this instance.

ECHO %%1 > TEST.txt

   This will send "%1" to the file "TEST.txt" and not the designated text
as may have been assigned to "%1". "%1" will not be replaced in this
instance, either.

    To show two percent signs with the FOR command, use "%%%" and your variable designator. Thus one may:


ECHO FOR %%%F IN (*.TXT *.DOC) DO COPY %%%F A: > TEST.txt

    Again, these examples may not be the case for your DOS version.
Experiment and test as to whether extra percent signs are needed.


 % Is there a Size Limit to a Variable?
    The total limit is a minimum of 128 characters but newer versions of DOS allow more. (4DOS allows a total of 511 characters.) The old limit means that the variable name and its contents could be any combination up to 123 characters long, with the balance being taken up by "SET", a space and the "equals" sign.

    However, writing excessively long variable lines could take away some of the shortcut nature of variables, but they may have a use with long Internet URLs or for long server designations on a network. They might also be useful to some for deeply-nested subdirectories or for very long file names on LFN-capable systems.



This wraps up this discussion on variables. There is much
more to this, including other ways to use the environment,
but the preceding article will give you an idea about, and
a start to, using DOS variables.




Employing environmental variables is yet another lane on
the road to becoming a DOS Power User. Learning about
them and incorporating them into your DOS repertoire
will improve productivity and allow you to automate
much more complicated tasks and procedures.




Main DOS Page