中国DOS联盟论坛

China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --
Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
Guest | Log in | Register | Members | Search | China DOS Union
中国DOS联盟论坛
The time now is 2026-08-04 09:08
48,038 topics / 350,123 posts / today 0 new / 48,251 members
DOS学习入门 & 精彩文章 (教学室) » Repost: DOS Environment Variables "Family Portrait"
Printable Version  4,861 / 20
Floor1 MYS Posted 2002-10-30 00:00
元老会员 Posts 1,637 Credits 5,170 From 广东佛山
DOS Environment Variables "Family Portrait"

The environment is the variable storage area of the DOS system - a high-speed temporary memory, which is stored in RAM and is the place where the DOS system stores important information for batch files. Users can modify or change its information as needed. Many DOS reference books ignore it, but whether you know it or not, you are using it consciously or unconsciously.

### How to Visit the Environment
There are two ways to visit the environment. One is to view it through the SET command, and the other is to use the DOS system's DEBUG command to view the computer's RAM. The SET command is intuitive and convenient, and most DOS users like to use it to set and view the DOS environment.

#### 1. Using the SET Command
The SET command can be used to set, modify, and view environment variables. Typing the SET command without parameters can display the current environment settings. These settings generally include the COMSPEC and PATH environment variables, which MS-DOS uses to find programs on the disk. There are also other environment variables used by the DOS system, such as PROMPT, DIRCMD, COPYCMD, etc. These environment variables are mostly set and generated using the SET command in the AUTOEXEC.BAT or CONFIG.SYS file. For example:

```
C:\>SET
COMSPEC=C:\COMMAND.COM
PROMPT=$p$g
PATH=C:\WINDOWS;C:\;C:\DOS;C:\FOXPRO25;C:\UCDOS;C:\GYPC;C:\CCED;C:\HD;C:\SARP
TEMP=C:\DOS
```

#### 2. Through the DEBUG Command
DEBUG is a powerful tool of the DOS-provided debugging program. It can be used to test disk raw information, view memory units, disassemble program files, or generate new programs, etc. However, the DEBUG command has certain risks, so users need to be careful during use.

Every time the DOS system runs a program, it generates a 256-byte memory block - the Program Segment Prefix (PSP), which contains some important information, such as memory units, vectors, the file name typed on the command line, the command line itself (a copy of the user's command line to run the program), and information on the storage locations of various reserved buffers. As long as it involves the environment, one of those storage locations will store the system environment address.

To view the environment under the DEBUG command state, first view the two bytes at offset 2C:

```
-D2CL2
1B41:0020  90 09
```

The data 90 09 is the segment address value of the computer environment obtained on a certain system (users should use the actual displayed number when operating). Reversing the order of these two pairs of numbers and following them with offset 0 can display the system environment under the DEBUG command. For example:

```
-D0990:0
0990:0000 43 4F 4D 53 50 45 43 3D-43 3A 5C 43 4F 4D 4D 41 COMSPEC=C:\COMMA
0990:0010 4E 44 2E 43 4F 4D 00 50-52 4F 4D 50 54 3D 24 70 ND.COM.PROMPT=$p
0090:0020 24 67 00 50 41 54 48 3D-43 3A 5C 57 49 4E 44 4F $g.PATH=C:\WINDO
0990:0030 57 53 3B 43 3A 5C 3B 43-3A 5C 44 4F 53 3B 43 3A WS;C:\;C:\DOS;C:\
0990:0040 5C 46 4F 58 50 52 4F 32-35 3B 43 3A 5C 55 43 44 \FOXPRO25;C:\UCD
0990:0050 4F 53 3B 43 3A 5C 47 59-50 43 3B 43 3A 5C 43 43 OS;C:\GYPC;C:\CC
0990:0060 45 44 3B 43 3A 5C 48 44-3B 43 3A 5C 53 41 52 50 ED;C:\HD;C:\SARP
0990:0070 00 54 45 4D 50 3D 43 3A-5C 44 4F 53 00 00 01 00 .TEMP=C:\DOS...
```

From the displayed environment above, each item in the environment is separated by the "empty" byte 00, and there are two null characters after the last item in the environment.

The results of the environment displayed by the above two methods are consistent.

### Environment Variables "Portrait"
#### 1. COMSPEC Variable
This variable specifies the location of the COMMAND.COM file. Usually, if the computer is booted from a hard disk drive, the COMSPEC variable is expressed as:

```
COMSPEC=C:\COMMAND.COM
```

If it is booted from a floppy disk drive, the COMSPEC variable is expressed as:

```
COMSPEC=A:\COMMAND.COM
```

The SET command can be used to change the location of COMSPEC. The format is:

```
SET COMSPEC=C:\DOS\COMMAND.COM
```

This command resets COMSPEC in the system environment to the COMMAND.COM file in the C drive DOS directory. Because COMMAND.COM does not need to be in the user's root directory. Users can also put COMMAND.COM into other subdirectories, which can be done using the SHELL command in CONFIG.SYS. During the computer startup process, CONFIG.SYS runs before COMMAND.COM is loaded. Using the SHELL command to notify the DOS system that COMMAND.COM is in another directory, the system will look for it in that directory. This has an obvious advantage, which keeps the root directory clean.

In addition, to ensure the normal operation of the system, users also need to add the following sentence in the CONFIG.SYS file:

```
SHELL=C:\DOS\COMMAND.COM /P
```

This command instructs DOS to find and run the command interpreter or shell program COMMAND.COM in the C:\DOS subdirectory. If this command is added to the CONFIG.SYS file, COMMAND.COM can be moved to the DOS subdirectory in advance. The P option instructs COMMAND.COM to find the AUTOEXEC.BAT file in the root directory and run it. Without this option, the AUTOEXEC.BAT file in the root directory cannot be run.

When the DOS system has no more space to store environment variables, the information "Out of environment space" will be seen. After this situation occurs, the SHELL command can be used to increase the environment space of COMMAND.COM. The optional E:1024 is to expand the environment space to 1KB (1024 bytes).

#### 2. PROMPT Variable
This environment variable displays the command prompt set by the user. The DOS system prompt is generally set to display the current drive and path followed by a ">" symbol. Of course, it can also be set to other types of command prompts. This variable is generally set in the AUTOEXEC.BAT file. The general format of the command is:

```
PROMPT $p$g
```

Of course, you can also use SET PROMPT=$p$g.

#### 3. PATH Variable
This environment variable includes setting the path using the PATH command, so that the DOS system can search for executable files in the specified directory.

#### 4. DIRCMD Variable
This environment variable allows the user to determine the default switches and parameter sets used by the DIR command. The user can use the SET command in the AUTOEXEC.BAT file or at the command prompt to define the DIRCMD environment variable, and pre-set the legal combination of DIR parameters and options (which can include paths and file names).

For example, to set the DIRCMD environment variable to the default format of pausing when full screen (/P), type the following in the AUTOEXEC.BAT batch file or the DOS command prompt:

```
SET DIRCMD=/P
```

At this time, when using the DIR command, it will pause when full screen and prompt "Press any key to continue".

If you don't want to use this default format, you can use it on the command line:

```
DIR /-P
```

If you want to clear this default setting, type the following at the command prompt:

```
SET DIRCMD=
```

#### 5. COPYCMD Variable
The user can set the COPYCMD environment variable to specify whether the COPY, MOVE, and XCOPY commands give a prompt first and then overwrite the file after confirmation.

If you force the COPY, MOVE, and XCOPY commands to give a prompt in all cases:

```
Overwrite Filename(YES/NO/ALL)?
```

Set the COPYCMD environment variable to /-Y (SET COPYCMD=/-Y). The user can choose whether to overwrite according to needs. You can also force the COPY, MOVE, and XCOPY commands to overwrite without prompting in all cases, then set the COPYCMD environment variable to /Y (SET COPYCMD=/Y). The COPYCMD environment variable set here takes precedence over all default values and current values of the COPYCMD environment variable.

#### 6. TEMP Variable
TEMP is a commonly used environment variable. It tells the program where to create temporary files, and some programs need to use the environment variable to identify the directory they want to use. For example:

```
SET TEMP=C:\DOS
```

In this example, the environment variable TEMP is used by the DOS environment and some other programs to determine the location of the current file subdirectory. The above directory C:\DOS is put into the environment, and now the DOS system knows where to put its current files. In the AUTOEXEC.BAT file, the user can and should set the entire conversion of the environment.

### The Close Relationship Between Environment Variables and Batch Files
Using environment variables can control certain batch files and make programs work according to the user's wishes, and can control the way MS-DOS displays and works. Generally, the user environment is set using the SET command in the AUTOEXEC.BAT or CONFIG.SYS file, so that the system can automatically set the environment variables according to the user's needs every time the computer is started.

#### 1. Calling Environment Variables in Batch Files
When calling the value of an environment variable from a batch file, the variable value must be enclosed in percent signs (%). For example, set a variable named WIN32 to be equal to the string C:\Windows\SYSTEM (the directory on the C drive), you can type the following command:

```
SET WIN32=C:\WINDOWS\SYSTEM
```

In the batch file, %WIN32% can be used instead of C:\WINDOWS\SYSTEM. Including the following command in the batch file displays the directory content of the C:\WINDOWS\SYSTEM environment variable:

```
DIR %WIN32%
```

When MS-DOS processes this command, it will replace %WIN32% with the string C:\WINDOWS\SYSTEM.

#### 2. Saving and Restoring the Original Path in the Environment in Batch Files
For each batch file, the user can modify the environment variable, and each batch file needs a different PATH to indicate the path to execute the program under the batch file. This requires the user to save the original path in the environment. The user can use an environment variable in the batch file to temporarily store the user's original path, so that it can be restored when needed without re-setting with the PATH command:

```
SET OLDPATH=%PATH%
```

For example, the settings of a certain computer are as follows:

```
SET OLDPATH=C:\WINDOWS;C:\; C:\DOS;C:\FOXPRO25; C:\UCDOS; C:\GYPC; C:\CCED;C:\HD; C:\SARP
```

The batch file interpreter expands the %PATH% variable into the user's current path, so the OLDPATH variable is equal to this path. If the system disrupts the original system path due to using other batch files, you can simply use the following statement in the batch file to restore the original path to meet the user's needs for different environments. For example:

```
PATH %OLDPATH%
```

The computer displays its execution situation:

```
PATH C:\WINDOWS;C:\;C:\DOS;C:\FOXPRO25;C:\UCDOS;C:\GYPC;C:\CCED;C:\HD;C:\SARP
```

In fact, many users may have the "OLDPATH" variable in their respective AUTOEXEC.BAT files. Since this environment variable always contains a backup of the original path, the original path can be easily restored.

### Summary
The environment is one of the very important contents of the DOS system. It is the variable storage area - a high-speed temporary memory - where the DOS system stores important information for batch files to use. It stores important information here, and users can put the environment variables of batch files into it.

The SET command is quite important for the environment. It is used to assign values to variables, clear variables, and display the environment content.

In addition to the user-generated environment variable (COMSPEC) giving the location of COMMAND.COM, PATH sets the system's search path; PROMPT sets the system prompt; the COPYCMD environment variable specifies whether the COPY, MOVE, and XCOPY commands prompt for files to be overwritten; the DIRCMD environment variable can pre-set DIR parameters and switches.

Since the environment table is quite small, the SHELL command can be used to expand its capacity. Although this avoids the error "Out of environment space", it also occupies more system RAM.
Floor2 yiyesong Posted 2002-11-01 00:00
元老会员 Posts 632 Credits 1,987
Well written, quite popularizing. Hope to write several more such articles. Is Teacher Mo teaching computer classes?
Floor3 MYS Posted 2002-11-01 00:00
元老会员 Posts 1,637 Credits 5,170 From 广东佛山
This is not what I wrote, it's reposted from the internet
Floor4 laochang409 Posted 2004-06-23 00:00
中级用户 Posts 44 Credits 297
Floor5 linux1234 Posted 2004-07-13 00:00
中级用户 Posts 42 Credits 293
Study hard and make progress every day
Floor6 jzhupo Posted 2004-07-16 00:00
中级用户 Posts 53 Credits 347
It seems I've seen it in "Computer Post"
Floor7 JonePeng Posted 2004-07-26 00:00
金牌会员 Posts 1,883 Credits 4,562 From 广东广州
No matter where it comes from, good things should be shared together!
Floor8 qcqzlj Posted 2006-08-16 17:08
初级用户 Posts 5 Credits 114
Good article, good article, solved a lot of my doubts in my heart, thank you
Floor9 electronixtar Posted 2006-08-16 17:26
铂金会员 Posts 2,672 Credits 7,493
Now the set under XP is much richer!
Floor10 IceCrack Posted 2006-08-17 08:12
中级用户 Posts 168 Credits 332 From 天涯
How bold a person is, the output of the cmd (command prompt) can be as productive.
Floor11 quhao441673011 Posted 2007-04-18 09:26
初级用户 Posts 10 Credits 30 From 仙剑国度
It's good, but I can't understand it. Who can help me?
Floor12 bw070 Posted 2007-04-19 02:07
中级用户 Posts 137 Credits 292
Saved for study...
Floor13 dqls889 Posted 2007-05-15 02:16
初级用户 Posts 36 Credits 68
Thanks
Floor14 joinhoone Posted 2007-05-17 14:53
初级用户 Posts 39 Credits 73
It's written really well. It's easy to understand. Hope all the masters who write articles can learn from it.
Floor15 expo123 Posted 2007-05-17 15:38
新手上路 Posts 1 Credits 2
Good
1 2  Next
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023