取消批处理脚本中 FOR /L 中的 SET

发布于 2025-01-08 05:50:36 字数 474 浏览 1 评论 0原文

p谁能解释一下为什么 SET 在下面的代码中没有做任何事情?

ECHO OFF
FOR /L %%b IN (-1,1,1) DO (
SET /A _lat=%%b
ECHO variable lat: %_lat%
ECHO variable b: %%b
ECHO:
)

: 输出:

variable lat:
variable b: -1

variable lat:
variable b: 0

variable lat:
variable b: 1

我尝试了没有 /A 的 SET 当然,我在 FOR 中尝试了其中变量 %%b 是来自文件的字符串并且它正常工作,但就在这里当变量 %%b 是一个变化的数字时,它不起作用。

如果您有任何建议,请告诉我。当然,我可以只使用 %%b 来显示我想要的内容,但由于这是较大脚本的一部分,为了使其更具可读性,我想使用适当的变量。

pCan anyone explain why SET is not doing anything in the following code?:

ECHO OFF
FOR /L %%b IN (-1,1,1) DO (
SET /A _lat=%%b
ECHO variable lat: %_lat%
ECHO variable b: %%b
ECHO:
)

output:

variable lat:
variable b: -1

variable lat:
variable b: 0

variable lat:
variable b: 1

I tried SET without /A of course, I tried that within FOR where variable %%b was a string from a file and it worked normally, but just here when variabl %%b is a changing number it does not work.

If you would have any suggestion, please tell me. Of couse I can use just %%b to diplay what I want, but since this is a part of larger scritp, to make it more readable, I would like to but the vaules to approproate variable.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

穿透光 2025-01-15 05:50:36

您需要使用ENABLEDELAYEDEXPANSION。尝试使用以下方法:

setlocal ENABLEDELAYEDEXPANSION

echo off
for /L %%b in (-1,1,1) do (
    set /A _lat=%%b
    echo variable lat: !_lat!
    echo variable b: %%b
    echo:
)

You need to use ENABLEDELAYEDEXPANSION. Try with the below:

setlocal ENABLEDELAYEDEXPANSION

echo off
for /L %%b in (-1,1,1) do (
    set /A _lat=%%b
    echo variable lat: !_lat!
    echo variable b: %%b
    echo:
)
因为看清所以看轻 2025-01-15 05:50:36

查看控制台中 SET /? 的输出。您需要启用并使用延迟环境变量扩展才能使其正常工作。以下是帮助集中的相关片段:

最后,支持延迟环境变量扩展
额外。默认情况下,此支持始终处于禁用状态,但可能会
通过 /V 命令行开关切换到 CMD.EXE 启用/禁用。参见 CMD /?

延迟环境变量扩展对于解决问题很有用
当前扩展的局限性,当线路
文本是被读取的,而不是在执行时。下面的例子
演示了立即变量扩展的问题:

设置 VAR=之前
如果“%VAR%”==“之前”(
    设置VAR=之后
    if "%VAR%" == "after" @echo 如果你看到这个,它就起作用了
)

永远不会显示该消息,因为两个 IF 语句中都有 %VAR%
当读取第一个 IF 语句时被替换,因为它在逻辑上
包括 IF 的主体,它是一个复合语句。所以
复合语句中的 IF 实际上是在比较“before”与
“之后”永远不会相等。同样,下面的例子
将无法按预期工作:

<前><代码>设置列表=
对于 (*) 中的 %i,请设置 LIST=%LIST% %i
回显%列表%

因为它不会在当前目录中建立文件列表,
而是将 LIST 变量设置为最后找到的文件。
同样,这是因为 %LIST% 仅在以下情况下扩展一次:
读取FOR语句,此时LIST变量为空。
所以我们正在执行的实际 FOR 循环是:

对于 (*) 中的 %i 设置 LIST= %i

它只是不断将 LIST 设置为找到的最后一个文件。

延迟环境变量扩展允许您使用不同的
字符(感叹号)来扩展环境变量
执行时间。如果启用延迟变量扩展,则上述
示例可以编写如下以按预期工作:

设置 VAR=之前
如果“%VAR%”==“之前”(
    设置VAR=之后
    如果“!VAR!” ==“after”@echo 如果你看到这个,它就起作用了
)

设置列表=
对于 (*) 中的 %i,请设置 LIST=!LIST! %我
回显%列表%

Take a look at the output of SET /? in a console. You need to enable and use delayed environment variable expansion for this to work. Here's the relevant snippet from the help set:

Finally, support for delayed environment variable expansion has been
added. This support is always disabled by default, but may be
enabled/disabled via the /V command line switch to CMD.EXE. See CMD /?

Delayed environment variable expansion is useful for getting around
the limitations of the current expansion which happens when a line
of text is read, not when it is executed. The following example
demonstrates the problem with immediate variable expansion:

set VAR=before
if "%VAR%" == "before" (
    set VAR=after
    if "%VAR%" == "after" @echo If you see this, it worked
)

would never display the message, since the %VAR% in BOTH IF statements
is substituted when the first IF statement is read, since it logically
includes the body of the IF, which is a compound statement. So the
IF inside the compound statement is really comparing "before" with
"after" which will never be equal. Similarly, the following example
will not work as expected:

set LIST=
for %i in (*) do set LIST=%LIST% %i
echo %LIST%

in that it will NOT build up a list of files in the current directory,
but instead will just set the LIST variable to the last file found.
Again, this is because the %LIST% is expanded just once when the
FOR statement is read, and at that time the LIST variable is empty.
So the actual FOR loop we are executing is:

for %i in (*) do set LIST= %i

which just keeps setting LIST to the last file found.

Delayed environment variable expansion allows you to use a different
character (the exclamation mark) to expand environment variables at
execution time. If delayed variable expansion is enabled, the above
examples could be written as follows to work as intended:

set VAR=before
if "%VAR%" == "before" (
    set VAR=after
    if "!VAR!" == "after" @echo If you see this, it worked
)

set LIST=
for %i in (*) do set LIST=!LIST! %i
echo %LIST%
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文