![]() |
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-05 17:57 |
48,039 topics / 350,124 posts / today 2 new / 48,251 members |
| DOS批处理 & 脚本技术(批处理室) » Under what circumstances should variable delay be used? |
| Printable Version 38,938 / 58 |
| Floor1 namejm | Posted 2006-05-21 12:30 |
| 荣誉版主 Posts 1,737 Credits 5,226 From 成都 | |
|
In what situations should variable delays be used? What is the format? What is the role of variable delays?
|
|
| Floor2 willsort | Posted 2006-05-22 14:08 |
| 元老会员 Posts 1,512 Credits 4,432 | |
|
───────────────── 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 ] |
|
| Floor3 namejm | Posted 2006-05-22 23:14 |
| 荣誉版主 Posts 1,737 Credits 5,226 From 成都 | |
|
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. |
|
| Floor4 willsort | Posted 2006-05-23 00:23 |
| 元老会员 Posts 1,512 Credits 4,432 | |
|
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. |
|
| Floor5 namejm | Posted 2006-05-23 00:34 |
| 荣誉版主 Posts 1,737 Credits 5,226 From 成都 | |
|
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.
|
|
| Floor6 lxmxn | Posted 2006-09-22 23:43 |
| 版主 Posts 4,938 Credits 11,386 | |
|
Both of you are so sincere, so I really admire you, junior brother.
|
|
| Floor7 jieok3375 | Posted 2006-10-19 04:57 |
| 中级用户 Posts 130 Credits 282 From 广东 | |
|
It seems to have a bit of understanding.
|
|
| Floor8 不得不爱 | Posted 2006-10-19 08:41 |
| 超级版主 Posts 2,044 Credits 5,310 From 四川南充 | |
|
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%! |
|
| Floor9 hake | Posted 2006-12-29 06:13 |
| 初级用户 Posts 43 Credits 103 | |
|
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? |
|
| Floor10 ccwan | Posted 2006-12-29 06:16 |
| 金牌会员 Posts 1,160 Credits 2,725 From 河北廊坊 | |
|
May I ask what doubts the person above has? You can say it out and everyone will help you.
|
|
| Floor11 scriptor | Posted 2007-01-13 11:04 |
| 银牌会员 Posts 555 Credits 1,187 | |
|
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
|
|
| Floor12 g4rr | Posted 2007-01-28 15:59 |
| 初级用户 Posts 32 Credits 68 From 广东潮州 | |
|
Should be understood. Thanks!
|
|
| Floor13 gues1688 | Posted 2007-01-29 02:43 |
| 新手上路 Posts 5 Credits 8 | |
|
What is delay, finally I know. Thanks willsort!
|
|
| Floor14 hngaoshou | Posted 2007-02-01 02:51 |
| 社区乞丐 Posts 77 Credits -16 | |
|
???
++++++++++Points |
|
| Floor15 jmzsyt | Posted 2007-03-02 02:28 |
| 新手上路 Posts 5 Credits 10 | |
|
Bump up~ Too high-tech, can't understand
|
|
| 1 2 3 4 Next |
|
[ Contact the Union admin team -
中国DOS联盟 -
Standard version ] Sponsored by ifanr Inc | © 2001–2023 |