[Summary] Windows XP-how to find files based on environment variables?

Source: Internet
Author: User

For the path environment variable, we use the most. Here we take this as an example:

 

If we input a link.exe In the example, how does the system find the corresponding link.exe to execute?

 

I have always wanted to find the exact answer to this question, but now I have only obtained several segments in the chain.

 

The following describes the process of trying to get a glimpse of the whole picture. I hope you can add the missing links to link this chain.

 

 

+ Known Part 1

The first step --First, find it in the current directory(This step should be correct, I can be sure)

If link.exe is found in the current directory, you can directly execute this executable file without further searching.

 

 

+ Known Segment 2

If it is not found in the current directory, the system will go to an environment variable to find the path variable, and then obtain some paths from it.

When you get these links, the system will find link.exe from these locations according to a sequence:

If it is found, it will be executed immediately and will not continue searching; otherwise, it will continue searching until all the paths are found.

 

Question:

 -Question 1.What are environment variables?

-Question 2.Where does the value in the environment variable come from? How is the final value provided to the program determined?

 -Question 3.In what order will the system search for "Some paths" in sequence?

 


+ Known part 3(Answer:Question 1)


What are environment variables?

Environment variables are specially named aliases or placeholders for certain basic system properties that are present for convenience in programming and in system administration.

-- From Environment Variables in Windows XP

For more information, see:

Environment variables are special variables.(Here I will directly translate the alias/flag into a variable)
They save some basic system attributes.
These attributes are generally used to facilitate programs or system administrators.

The environment variables generally store information such as paths, file names, and system parameter values.
When the program needs these values, it can ask for environment variables.

Since environment variables can provide information for programs, they can affect program behavior.
For example, in the first example, the commander executes link.exe. if link.exe is not found in this directory, the subsequent behavior of the commander is affected by the environment variable.


A convention:

In order to be different from other environment variables that may be discussed below, we refer to the so-called "environment variables that can affect program behavior" as"Final environment variable".

Other things, also known as "environment variables", are described as parameters that affect the final environment variables.


How do I know which environment variables can be used by the current program when the program is running? What are the values in these variables?

For general programs, there should be corresponding APIs to obtain these values. I am not very clear about them. Here we will make a mark and then add it. Mark]
For a batch processing program, you can use the set tool provided by windows to output all the environment variables and the values in the batch processing program.

For example:
-------------

[Mark] Here is an example to be added.

---------------



+ Known segment 4(Answer:Question 2)

 

So far, we know what environment variables are and how they are finally used by the program.

HoweverWhere do these variables and their values come from? How is the final value determined?

 

Factors that affect the final environment variables and their values

According to the MS document and other information on the Internet, I have summarized the possible impact of Windows XP.Final environment variableThere are five parts in total:

  1. SystemHidden-variablesSystem preset dynamic environment variables
  2. System-variablesSystem environment variables (Admin can be set through the system environment variables dialog box)
  3. SystemAutoexec-variablesStart environment variable (the environment variable set by autoexec. bat is read when the system starts)
  4. CurrentUser-variablesCurrent user environment variable (you can set it in the system environment variable dialog box)
  5. ProgramSet-variablesEnvironment variables in the current program (environment variables set by the current program/batch processing through set)

Note: In addition to these five types, there is also a kind of "dynamic environment variables automatically extended by the system according to the running state" (see [Supplement 1] at the end of this Article ])
However, since they do not overlap with the final environment variables and do not affect the final environment variables, they are not listed here.

The following describes the five environment variables:

  • 1. systemHidden-variablesSystem preset dynamic environment variables 

The complete definition and explanation of such environment variables are not found on the Internet.
The MS documentation mentions the concepts of "default system variables" and "Dynamic variables"
Let me start with a name like this.

Since Ms does not have a clear definition, why should I list such an environment variable separately and "Climb relatives" in the MS document?
When I used set to view the final environment variables, I found several environment variables that are "special:

ALLUSERSPROFILE = C:/Documents ents and Settings/All Users
APPDATA = G:/myProfile/Application Data
CommonProgramFiles = C:/Program Files/Common Files
COMPUTERNAME = MYNAME
HOMEDRIVE = C:
HOMEPATH =/Documents ents and Settings/mynick
LOGONSERVER = // MYNAME
ProgramFiles = C:/Program Files
PROMPT = $ P $ G
SESSIONNAME = Console
SystemDrive = C:
SystemRoot = C:/WINDOWS
USERDOMAIN = MYNAME
USERNAME = mynick

What are their "special "?
Originally,In "my computer-properties-advanced-environment variables", these values cannot be found in system variables or user variables..

Since they cannot be found, I suspect they are different from the environment variables that can be viewed on the setting interface and may be stored in different places.
(Later I found that it was stored in different places. See [Supplement 2] at the end of this Article ])

In terms of content, the system reads and sets the information from some special points of the system during system initialization.
(What stage is it and where to obtain the information?) [Mark]

Based on the above facts, I classify them into one category.

  • 2.System-variablesSystem Environment Variables

These environment variables also have default values.
However, administrators can modify these variables in "system properties-advanced-environment variables-system variables.

  • 3. systemAutoexec-VariablesStart environment variable

During startup, the system can analyze the set statement in autoexec. BAT to set the environment variables.

As a matter of fact, many materials (document 1, document 2) refer to the environment variables set in this way when interpreting the order of environmental variables.
However, most of the information does not clarify the following fact:

"In NT systems such as 2000, XP, and 2003, the startup process (for windows) no longer depends on the config. sys/autoexec. bat files. Where, for config. sys is no longer in use. For autoexec. bat. Only when the ParseAutoexec value in the Registry (see reference) is 1 (this value is the default value) Will the set/path and other environment variable setting statements be analyzed, other statements are ignored in any case. "

Quote:
User Key: [HKEY_CURRENT_USER/software/Microsoft/Windows NT/CurrentVersion/Winlogon]

Value Name: parseautoexec

Data Type: REG_SZ (string value)

Value Data: (0 = disabled, 1 = enabled)

-- From cn-dos this post, by honorary moderator willsort

 

  • 4.Current user-VariablesCurrent user environment variable

You can set the parameters in "system properties-advanced-environment variables-system variables.

  • 5. programSet-VariablesEnvironment variables in the current program

In batch processing, you can use the set command to set internal environment variables for the current process.

For the format and usage of the SET command, see: Set

 

In what order and rules do various factors obtain the final environment variable?

This is explained in the MS documentation.

Variables are processed in the following order:

  1. Dynamic variables such as operating system variables.
  2. System variables defined in the System tool in Control Panel.
  3. Variables defined in the Autoexec. bat file.
  4. Environment variables defined in the System tool in Control panel for the current user.

NOTE: If the same variable is defined more than once, the value of the variable is the value that is set last.

-- Environment Variable Processing Order in Windows 2000, Q185652

However, this document applies to "Windows 2000 Server & Microsoft Windows 2000 Professional Edition"
I don't know if Windows XP also applies to this first-class process and feature. To be verified! Mark]

Here, let's take a look at the MS documentation process:

In fact, the order of the five factors described above is exactly the same:

  1. Hidden-variables
  2. System-variables
  3. Autoexec-variables
  4. User-Variables
  5. Set-Variables

Top-down, Extract key-value from these variablesFinal environment variable
If a key-value is defined more than once in these factors
ThenThe last defined key-value prevails.

We call it an overwrite rule.

 

[Update] Found hereOne exceptionOfAppend rules

If path is defined in user-variables

The path does not overwrite the value defined in system-variables.

Instead, the path will be added after it

 

Example:

If the path in system-variables is set to "C:/folderA; C:/folderB"

In user-variables, the path is set to "C:/folderC"

 

If set-variables is not set next

In the final environment variable, path is "C:/folderA; C:/folderB; C:/folderC"

 

Of course, if set-variable is used to overwrite the path

According to the overwrite rule, the final path is still based on the set path.

 


+ Fragment 5(Answer:Question 3)

We have learned how the final environment variable value came from above.

In this example, we have learned that when link.exe is not found, we will find the environment variable path.
We also know how the values in path come from.

Next, with this set of paths,In what order do I find the path?
From left to right, or from right to left?

The answer is:From left to right

+ Known fragment 6 


Which of the following is the execution of different extensions with the same name?

In the preceding example, you need to find link.exe explicitly.
If the link is entered in cmd without the extension, how can the system find the link after obtaining the path ??

The answer is:Determine the sequence of extension names based on the value in the environment variable "PATHEXT "..

For example, the value in "PATHEXT" is:
. COM;. EXE;. BAT;. CMD
If the directory contains link. COM link. EXE link. BAT link. CMD
Enter link in the command line and execute link. COM.

 

 


Over!

 

========================================================== ==============

[Supplement 1]

The following areDynamically scalable environment variables based on the status of the current system or program.

% CD %-extended to the current directory string.
% DATE %-extend to the current DATE in the same format as the DATE command.
% TIME %-extend to the current TIME in the same format as the TIME command.
% RANDOM %-any decimal number between 0 and 32767.
% ERRORLEVEL %-extend to the current ERRORLEVEL value.
% CMDEXTVERSION %-extended to the current command processor extension version number.
% Worker line %-extended to the original command line that calls the command processor.

 

---------------------------------------------------------------------

[Supplement 2]

Environment variables in the Registry

See the system environment variables and user environment variables and environment variables that are invisible but have system default values.
I have made some guesses about the sources of these two variables and think that they may be stored in different places ".

After checking the registry, I found that --

ThoseSystem variables, All in the Registry here:

[HKEY_LOCAL_MACHINE/SYSTEM/CurrentControlSet/Control/Session Manager/Environment]
(Content omitted)

ThoseCurrentUser variables, All in the Registry here:

[HKEY_CURRENT_USER/Environment]
(Content omitted)

In those initial situations, there are default values,Default system variables that cannot be viewed, The set description is not found in the registry:

This key is the largest

[HKEY_CURRENT_USER/Volatile Environment]
"LOGONSERVER" = "// MYNAME"
"CLIENTNAME" = ""
"SESSIONNAME" = "Console"
"Appdata" = "G: // myprofile // Application Data"
"Homedrive" = "C :"
"Homeshare" = ""
"Homepath" = "// Documents and Settings // mynick"

[HKEY_LOCAL_MACHINE/system/controlset001/control/computername]
"Computername" = "myname"

Since these groups are all user-related variables, I think they are written to the Registry and environment variables only when a user logs on.

The others are not found in the registry:
USERDOMAIN
This is the computer name set by the user. I guess it was determined when the system was installed.

SystemDrive
I guess it was determined during system installation.

SystemRoot
This is determined by the system type. windowsXP is % SystemDrive %/WINDOWS

PROMPT
It is suspected that the system defaults to $ P $ G (that is, current_drive_and_path> is used by default in cmd, see PROMPT)

ALLUSERSPROFILE
There is no exact match in the registry, but the following items can be combined:

[HKEY_LOCAL_MACHINE/software/Microsoft/Windows NT/CurrentVersion/profilelist]
"Profilesdirectory" = "% systemdrive %/Documents and Settings"
"Defaultuserprofile" = "Default User"
"Allusersprofile" = "all users"

MS describes the default system variables.

During installation, Windows XP setup configures the default system variables, such as the path to the Windows files.
-- How to manage environment variables in Windows XP, MS q310519

I think the following variables that are not found in the Registry are as follows: windowxp has been fixed during installation and initialization.

[Supplement 3]

In the system environment variables dialog box, when does it take effect after you modify the system variables or user variables?

 

Many materials say that the system needs to be restarted, and some say that you just need to restart a cmd.

I verified it and found that:

All changes to environment variables have no effect on the cmd process that was originally started.

However, you only need to re-run the cmd command to see the modification effect.

 

 

------------------------------------------------------------

Powered by ScribeFire.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.