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-08-04 03:35
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Question about set View 670 Replies 2
Original Poster Posted 2007-04-14 00:17 ·  中国 河北 廊坊 联通
初级用户
Credits 108
Posts 35
Joined 2007-04-12 15:23
19-year member
UID 84897
Gender Male
From 河北廊坊
Status Offline
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?
Floor 2 Posted 2007-04-14 02:48 ·  中国 上海 浦东新区 电信
中级用户
★★
Credits 302
Posts 138
Joined 2007-03-29 02:33
19-year member
UID 83224
Gender Male
Status Offline
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.
Floor 3 Posted 2007-04-14 03:00 ·  中国 广东 广州 天河区 电信
高级用户
★★★
潜水修练批处理
Credits 788
Posts 366
Joined 2006-12-31 02:43
19-year member
UID 75048
Gender Male
Status Offline
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.
Forum Jump: