The DOS Environment By Ken Johnson, The DOS environment is a special area of memory that DOS uses as a "cubbyhole" to store information needed at a later time. Knowing what it is and how it is used is becoming important, as software programs and batch files get more sophisticated and make more use of the environment. This month we'll look at the environment and environment variables. The DOS environment contains string (text) variables that DOS, some application programs, and batch files can check and use. Environment variables that DOS uses include PROMPT, PATH, COMSPEC, and DIRCMD, which we'll discuss below. Application programs can create and use a variety of environment variables. Three examples on my computers are TEMP (defines the Windows 3.0 directory for temporary files), GMK4 (defines the Grammatik IV subdirectory), and 123CPBW (indicates whether Lotus 123 release 3.x is running on a monochrome monitor). The SET command is used to create and delete environment variables. Environment variables are "global", since once created the can be used by any program or batch file (though they can't be used on the command line). Usually SET commands are included in the AUTOEXEC.BAT file so that environment variables will be created each time you boot up your computer. Any application program that uses environment variables will usually add the SET statement to your AUTOEXEC.BAT file when it's installed. The form of the SET command is: SET variable=value where "variable" is the variable name and "value" is the text string it represents. Some examples are: SET DIRCMD=/OE SET TEMP=C:\WINDOWS\TEMP SET COMSPEC=C:\DOS\COMMAND.COM SET 123CPBW=YES The variable name is always converted to upper case, but the value remains as it was entered. Also be aware the names are space sensitive, so "SET USER=JOE" is not the same as "SET USER = JOE". To remove an environment variable, enter SET variable= with no value specified. By entering "SET" alone on the command line, you'll see a listing of what's in the environment. This will include your current PATH and PROMPT, the name and path of your command interpreter (i.e., COMMAND.COM) in the COMSPEC variable, and any other environment variables that have been created. Now let's look at some environmental variables and how they are used. The COMSPEC Variable The COMSPEC environment variable points to the location (drive and directory) of your command interpreter, usually COMMAND.COM. This is important because COMMAND.COM loads into memory in two parts, a permanent portion and a transient portion. The transient part of COMMAND.COM can be removed from RAM if an application program needs that memory. When the program ends, DOS needs to reload COMMAND.COM to continue. The COMSPEC variable tells DOS where to find COMMAND.COM if it needs to be reloaded. If DOS can't find COMMAND.COM, you'll get a "Cannot load COMMAND, system halted" message and have to reboot. Normally the COMSPEC variable is set at the beginning of your AUTOEXEC.BAT file, but the preferred alternative is to use the SHELL= statement in CONFIG.SYS to point to COMMAND.COM. We'll discuss the SHELL statement below. The PATH command/variable The PATH is a listing of the drives and directories that DOS should search when you enter the name of an executable file on the command line. DOS will always look in the current (default) directory first, then search the directories listed in the PATH in the order specified. The PATH is specified as: PATH=[d:]\directory;[d:]\directory; . . . where "d:" is an option drive identifier and "directory" is the directory name; you separate the directory names with a semicolon. The total length of the PATH statement must be less than 128 characters, since the DOS command line is limited to 127 characters. Notice that though PATH is an environment variable, you don't have to enter it with the SET command. Both "SET PATH= . . ." and "PATH= . . ." will work. Since the path is searched in the order specified, make sure to put the directories containing your most used programs at the beginning. For example, PATH=C:\DOS;C:\;C:\UTILITY shows that DOS will search for a program in this order: 1. The default directory (always searched first, before the path is ever accessed) 2. The DOS subdirectory on the C: drive 3. The root directory of the C: drive (C:\) 4. The UTILITY subdirectory on the C: drive Another key to the PATH is to keep it as short as possible; in other words, don't put ALL your directories on the PATH. There are two reasons for this. First, since the PATH is an environment variable it takes up environment space that may be needed by other variables. In DOS 3.x for example, a 120 character PATH statement uses three-fourths of the default environmental space. Secondly, every time you make a typing mistake DOS will search the entire path before returning the "Bad command or file name" message. Longer paths mean more time to wait each time you make a mistake. Since the PATH is part of the environment, you can view the path with the other environment variables by entering "SET" at the DOS prompt. You also can enter "PATH" or "PATH=" to see just the currently defined PATH. To remove the PATH from the environment (thus forcing DOS only to search the current directory), enter either "PATH ;" or "SET PATH=" at the DOS prompt. The PROMPT command/variable You've seen the default DOS prompt hundreds of times: the infamous "C>". The PROMPT variable determines what prompt is displayed, so you can use it to change "C>" to something a little more user friendly. The prompt is specified as: PROMPT text where "text" is any string of text or special prompt characters. These special characters will display system information as part of the prompt: $P -- the default directory $T -- the current time (HH:MM:SS.xx) $D -- the current date (MM-DD-YY) $V -- the DOS version $N -- the default drive $_ -- Carriage Return plus Line Feed $E -- the ESCape character $H -- backspace (erases the previous character) $G -- the > character $L -- the < character $B -- the | character $Q -- the = character As you can see, the standard DOS prompt is $N$G, the default drive and the ">" sign. If you enter the PROMPT command with nothing after it, DOS resets the prompt to this default. As with the PATH, you do not have to use the SET statement with PROMPT. Some sample prompts: PROMPT $P$G will display the current drive and directory with the greater than sign. This is the most common PROMPT setting, since it shows you exactly where in the directory structure you are located. For example, if you are in the DOCS directory of the WP51 directory on your C: drive, your prompt will show: C:\WP51\DOCS>_ PROMPT Time is $T$H$H$H$H$H$H$_$P$_Command--$G will give a multi level prompt (the six $H are used to erase the seconds and hundredths from the time): Time is 10:35 C:\WP51\DOCS Command-->_ PROMPT will reset the prompt to the default ($N$G): C:> There is one minor problem when having DOS display the current directory ($P). Each time you press Enter, DOS will read the disk to find the correct directory for the prompt. If your default drive is a floppy drive and you take the disk out, DOS will try to read the empty drive and you'll get an error message: Not ready reading drive A: Abort, Retry, Fail? At this point you can put the disk back in and press R. Or you can simply press F, in which case your prompt will become: Current drive is no longer valid>_ Just change back to your hard drive and your old prompt will reappear. The DIRCMD variable In DOS 5.0, the DIRectory command now supports several new switches to control what files are displayed and in what order. For example, the /OGEN switch will display files sorted by extension and name, with directories listed first. Rather than have to remember (and type in) certain switches with every DIR command, you can use the DIRCMD variable to indicate which switches to be used by default. For example, SET DIRCMD=/OGEN will give you a directory sorted by extension and name (with directories first) every time you use the DIR command. The TEMP variable The TEMP variable is used most often to tell a program where to store temporary files. These are "working" files created as the program runs. Under normal operation, the program will delete these temporary files when finished with them. One such program is Windows 3.0, which uses a \TEMP subdirectory to store it's temporary files. When you install Windows it will add the appropriate SET TEMP statement to your AUTOEXEC.BAT file. Assuming Windows is on your C: drive, this probably will be SET TEMP=C:\WINDOWS\TEMP. One trick to speed up Windows is to use a RAM disk for the TEMP subdirectory. A RAM disk is an area of memory that is treated as a physical disk drive. You can set up a RAM disk by using a driver such as RAMDRIVE.SYS, which comes with Windows. Once you've installed the RAM disk, change the SET TEMP statement in your AUTOEXEC.BAT file to point the RAM disk's drive designator. For example, if the RAM drive were drive E:, specify SET TEMP=E:\. Since it is much faster to write to RAM than to a physical disk drive, using a RAM disk for Windows' TEMP subdirectory will improve Windows' performance. Another benefit of using a RAM disk for Windows' temporary files is that you'll be sure they are deleted when no longer needed. Windows normally deletes all temporary files, unless it ends abnormally. If you've every gotten an "Unrecoverable Application Error" and had to reboot your system, Windows may have left some temporary files in the \TEMP subdirectory. By using a RAM disk, the temporary files will be wiped out whenever you reboot or turn off your system. Using Environment Variables in Batch Files One powerful feature of environment variables is the ability to use them in batch files in DOS 3.2 and above. You surround the name of the variable with percent signs (for example, %PATH%); when the batch file executes, the variable name expands to it's value. Remember that the variable's value is case sensitive and space sensitive. For example, part of your batch file might include: [==> SHARON, please use smaller font so that lines are not split <==] SET DONE=false :LOOP . . (other commands here, one of which is SET DONE=true) . IF %DONE%==false GOTO LOOP Since the path is an environment variable, it is often used in batch files. For example, you can create two simple batch files that will add a directory to your current path in the first position, then restore the original path. These are NEWPATH.BAT and OLDPATH.BAT: [==> Note: please use smaller font so that lines are not split <==] ECHO OFF REM NEWPATH.BAT IF %1!==! GOTO NODIR SET OLD=%PATH% PATH=%1;%OLD% ECHO Your path is now %PATH% GOTO END :NODIR ECHO Please enter the directory to add to the path :END ECHO OFF REM OLDPATH.BAT IF %OLD%!==! GOTO END PATH=%OLD% SET OLD= :END The NEWPATH batch file first checks to make sure that a new directory was entered on the command line (which becomes replaceable parameter %1). It then stores the original path in a new environment variable called OLD. The PATH is redefined, with the new directory appended on the front of the old path (%1;%OLD%). The batch file will then display the new path with the ECHO command. When you are ready to restore the original path, simply execute the OLDPATH batch file. It uses the OLD environment variable as the new path, then deletes the OLD environmental variable. Notice that the batch file first checks whether there is an OLD environment variable, and simply branches to the end if it doesn't exist. Without an OLD environment variable the "PATH=%OLD%" statement would resolve as "PATH=", which would display the current path. Expanding the Size of the Environment with SHELL Since it is an area of RAM, it is possible to fill the memory allocated to the environment. This is particularly true if you have a long PATH and create several environment variables. In the NEWPATH batch file example above, you are significantly increasing the size of your environment because it now contains both the old path and the new path. If the environment does fill, you'll receive an "Out of Environment Space" message. This means you must increase the size of the environment. The default environment is 160 bytes in DOS 2.x, 3.x, and 4.x, and 256 bytes in DOS 5.0. In DOS 3.1 and above you can specify the desired environment size with the SHELL command in your CONFIG.SYS file. In DOS 2.x and 3.0, you can only increase the environment by modifying (also called "patching") the COMMAND.COM program -- not a job for the faint hearted! The SHELL statement in CONFIG.SYS tells DOS name of the command interpreter (usually COMMAND.COM), its location, and the size of the DOS environment. The SHELL statement usually looks something like this: SHELL=C:\DOS\COMMAND.COM C:\DOS /P /E:512 As you might guess, the "/E:nnn" switch sets the size of the environment, 512 bytes in this example. In DOS 3.2 and above, simply specify the size in bytes. If you are using DOS 3.1, the environment is specified in 16-byte blocks, so indicate the number of blocks on the /E switch. To get a 512 byte environment, include /E:32 on the SHELL command (16 bytes times 32 blocks = 512 bytes). Let's look at the other parts of the SHELL statement. "C:\DOS\COMMAND.COM" shows the name and location of the command interpreter. "C:\DOS" points to the directory where COMMAND.COM is located, so that it can be reloaded into memory if necessary. This will create the COMSPEC variable in the environment, meaning you don't have to explicitly SET it in your AUTOEXEC.BAT file. Finally, the "/P" switch indicates COMMAND.COM should remain permanently in memory. The DOS environment is one of those (perhaps few!) things with computers that you may never need to worry about. But making good use of the environment by setting a custom PROMPT, and efficient PATH, and a fast TEMP subdirectory can help you be a more productive PC user. Just keep an eye on your environment size!