China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-06-20 20:05
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Under what circumstances should variable delay be used? DigestI View 37,813 Replies 58
Original Poster Posted 2006-05-21 12:30 ·  中国 四川 成都 鹏博士宽带
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
In what situations should variable delays be used? What is the format? What is the role of variable delays?
Recent Ratings for This Post ( 3 in total) Click for details
RaterScoreTime
everest79 0 2007-03-26 10:39
pzr +1 2007-08-31 14:43
ab200210 +2 2007-10-01 16:40
Floor 2 Posted 2006-05-22 14:08 ·  中国 山西 大同 中移铁通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline

───────────────── 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
RaterScoreTime
haiou327 +4 2008-01-04 21:38
plp626 +4 2008-01-25 06:24
zyz0304360 +4 2008-02-10 01:14
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 3 Posted 2006-05-22 23:14 ·  中国 四川 成都 鹏博士宽带
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
Thanks to willsort for the detailed explanation. The Chinese help for `set /?` was a bit hard to understand. There were a few technical terms that were tough. After reading willsort's explanation, I realized that "expansion" is actually common actions like referencing and calling. It's like a light - bulb moment.

I was used to posting for help in the "DOS疑难解答 & 问题讨论(解答室)" section. After the new version was launched, I didn't adjust right away and made a mistake in posting to the wrong place, causing trouble for the moderators. I'm really sorry about that. But I feel a bit wronged about "deducting 2 points of punitive points for title violation". It seems my title didn't cause ambiguity or was too broad. If it's an additional punishment for posting in the wrong place, I have no objection, but the term "title violation" doesn't seem appropriate. This is just my personal opinion. I hope the moderators can give some advice.
Recent Ratings for This Post ( 2 in total) Click for details
RaterScoreTime
xiaohacker +2 2007-12-23 20:38
haiou327 +4 2008-01-04 21:36
Floor 4 Posted 2006-05-23 00:23 ·  中国 山西 运城 中移铁通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
Re namejm:

Thanks for carefully pointing out the typo in the moderation record. It has been corrected now. This version of the moderation record template has been in use for more than a month, and this is the first time someone has pointed out the typo. At the same time, moved by your sincerity, I will award 16 points of points to the reply on floor 3. At the same time, I ask other moderators in the Batch Processing Room to deduct 16 points of points from the reply on floor 2, because I can't rate my own reply.
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
hansom +2 2008-05-28 00:28
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 5 Posted 2006-05-23 00:34 ·  中国 四川 成都 鹏博士宽带
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
Hehe, I posted two wrong posts before, totaling 16 points deducted. Now I'm covered by the moderator's mercy and all are made up. Since it's the template's reason, it's not the moderator's fault. I request other moderators not to deduct points from willsort, and deeply admire willsort's responsible attitude of daring to blame oneself.
Floor 6 Posted 2006-09-22 23:43 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
19-year member
UID 59080
Status Offline
Both of you are so sincere, so I really admire you, junior brother.
Floor 7 Posted 2006-10-19 04:57 ·  中国 广东 东莞 电信
中级用户
★★
Credits 282
Posts 130
Joined 2006-09-20 22:25
19-year member
UID 63201
From 广东
Status Offline
It seems to have a bit of understanding.
~~~~好好学习
天天向上~~~~
Floor 8 Posted 2006-10-19 08:41 ·  中国 四川 南充 电信
超级版主
★★★★
我爱DOS
Credits 5,310
Posts 2,044
Joined 2005-09-26 12:00
20-year member
UID 42843
Gender Male
From 四川南充
Status Offline
There is also the case where when you need to use a variable inside another variable, you can use variable delay. For example:
set a=1000
set b=dd
set a%b%=9000
set c=!a%b%!
Floor 9 Posted 2006-12-29 06:13 ·  中国 江西 九江 电信
初级用户
Credits 103
Posts 43
Joined 2006-12-22 23:30
19-year member
UID 74302
Gender Male
Status Offline
Regarding environment variable delayed expansion, you can view some instructions using set /?. However, considering its poor translation level, it is recommended to first switch to English by chcp 437 to view the original English instructions. Since the text has been very detailed and there are several code examples, it should not be difficult to understand. Here, only a few supplements are made.

Can your Chinese be better?
Floor 10 Posted 2006-12-29 06:16 ·  中国 河北 廊坊 三河市 移动
金牌会员
★★★★
Credits 2,725
Posts 1,160
Joined 2006-09-23 12:00
19-year member
UID 63486
From 河北廊坊
Status Offline
May I ask what doubts the person above has? You can say it out and everyone will help you.
三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。
Floor 11 Posted 2007-01-13 11:04 ·  中国 北京 中国科学院研究生院
银牌会员
★★★
Credits 1,187
Posts 555
Joined 2006-12-21 07:35
19-year member
UID 74129
Gender Male
Status Offline
Whenever you need to call the variables you originally defined again after the for statement, you need to use variable delay. I finally solved a problem. Hehe
Floor 12 Posted 2007-01-28 15:59 ·  中国 广东 广州 番禺区 电信
初级用户
Credits 68
Posts 32
Joined 2007-01-24 04:00
19-year member
UID 77490
Gender Male
From 广东潮州
Status Offline
Should be understood. Thanks!
Floor 13 Posted 2007-01-29 02:43 ·  中国 福建 三明 电信
新手上路
Credits 8
Posts 5
Joined 2006-11-25 05:53
19-year member
UID 71670
Gender Male
Status Offline
What is delay, finally I know. Thanks willsort!
Floor 14 Posted 2007-02-01 02:51 ·  中国 湖南 长沙 联通
社区乞丐
★★
gaoshou
Credits -16
Posts 77
Joined 2007-01-30 09:11
19-year member
UID 78105
Gender Male
Status Offline
???
++++++++++Points
:cool:
Floor 15 Posted 2007-03-02 02:28 ·  中国 山东 青岛 联通
新手上路
Credits 10
Posts 5
Joined 2006-11-21 04:48
19-year member
UID 71206
Gender Male
Status Offline
Bump up~ Too high-tech, can't understand
Forum Jump: