───────────────── Moderation Record ─────────────────
Performed by: Will Sort
Operation: Move Topic: From "DOS Troubleshooting & Question Discussion (Help Desk)"
Description: According to topic content classification, it is more suitable to be posted in this forum area
Tip: The original area redirect link will be deleted after three days
Punishment: Deduct 6 points of points awarded for posting this topic, and deduct 2 points of points for forum area violation penalty
───────────────── Moderation Record ─────────────────
Re namejm:
Regarding environment variable delayed expansion, you can check some instructions using set /?. However, considering its crude translation level, it is recommended to first switch to English by chcp 437 to view the original English instructions before checking. Since the text has been described very detailedly and there are several code examples, it should not be difficult to understand. Only some supplements are made here.
In many visible official documents, the behavior of using a pair of percent signs to close an environment variable to complete the replacement of its value is called "expansion" (expansion), which is actually a first-party concept, referred to from the perspective of the command interpreter. From our user's perspective, it can be regarded as reference (Reference), call (Call) or get (Get).
And the behavior of the command interpreter to expand environment variables is roughly as follows: First, read a complete statement of the command line. After some preliminary preprocessing, before the command is interpreted and executed, it will match the string closed with percent signs. If a matching environment variable is found in the environment space, its value will be used to replace the original string and the percent sign itself. If no match is obtained, an empty string will be used for replacement. This process is the "expansion" of the environment variable, which still belongs to the preprocessing category of the command line.
And a "complete statement" is interpreted in the NT command interpreter CMD as statements containing statement blocks such as "for if else" and compound statements connected by "& | && ||" and so on.
Therefore, when CMD reads a for statement, all statements closed with a pair of parentheses after it will be read together, and necessary preprocessing work will be completed, including the expansion of environment variables. Therefore, before all statements in the for are executed, all environment variables have been replaced with the values set before the for, thus becoming a string constant, and no longer a variable. No matter how those environment variables are modified in the for, what is really affected is the environment variable space, not the inside of the for statement.
And in order to be able to perceive the dynamic changes of environment variables inside the for statement, CMD has designed the delayed environment variable expansion feature, that is, when CMD reads a complete statement, it will not immediately perform the variable expansion behavior, but will expand again before a single statement is executed, that is, this expansion behavior is "delayed".
The delayed environment variable expansion feature is off by default in CMD. There are currently two methods to turn it on: one is CMD /v:off ( There is a mistake here, it should be CMD /v:on——note by namejm ), which will open a new command line shell. Before using exit to exit this shell, the expansion feature is always effective, which is often used in the command line environment; the second is setlocal EnableDelayedExpansion, which will limit the modification of environment variables to the local space. After endlocal, the expansion feature and the previous modifications to environment variables will disappear together, which is often used in batch processing statements.
[ Last edited by namejm on 2007-3-17 at 09:01 PM ]
Recent Ratings for This Post
( 3 in total)
Click for details
※ Batchinger 致 Bat Fans:请访问
批处理编程的异类 ,欢迎交流与共享批处理编程心得!