中国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 04:39
48,038 topics / 350,123 posts / today 0 new / 48,251 members
DOS批处理 & 脚本技术(批处理室) » Question about set
Printable Version  672 / 2
Floor1 caucfeiyu Posted 2007-04-14 00:17
初级用户 Posts 35 Credits 108 From 河北廊坊
There is an example like this in the help:
set VAR=before
if "%VAR%" == "before" (
set VAR=after
if "%VAR%" == "after" @echo If you see this, it worked
)
It won't display anything. The explanation in the help says this:

The message will not be displayed, because when the first IF statement is read, the %VAR% in BOTH IF statements
is replaced; the reason is: it is within the body of the IF, and IF is a
compound statement. So, the IF inside the compound statement is actually comparing "before" and
"after", and those two can never be equal.
I don't really understand what that means. Could the experts explain it again?
Floor2 lililulula Posted 2007-04-14 02:48
中级用户 Posts 138 Credits 302
OP can search for the newbie tutorial thread about delayed variables that a friend posted before, then you'll understand it.
Roughly speaking, it means that because statements like if and for, before delayed variables are enabled, once they execute, changes to variable values are not judged (this is how I understand it). Here:
if "%VAR%" == "before" (
set VAR=after(not judged)
if "%VAR%" == "after" @echo If you see this, it worked(so the value of var here is still before, and as a result nothing will be displayed)
)
You'd better read that thread yourself; I'm just explaining it according to my own understanding.
Floor3 dikex Posted 2007-04-14 03:00
高级用户 Posts 366 Credits 788
My understanding is that, when delayed variable expansion is not enabled, before each statement is executed (a compound one counts as one statement), the variables inside it are first replaced and then that statement is executed. The variable will not be updated again until that statement finishes. In the example from the help: when executing this compound if statement, %VAR% is first completely replaced with before and then executed, so the statement the system executes is this:
if "before" == "before" (
set VAR=after
if "before" == "after" @echo If you see this, it worked
)

So nothing is displayed. If you've learned C, you'll find it very similar to the preprocessor directive #define.
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023