Windows批处理脚本-For/L不工作--简单
需要一些快速的帮助。这是一个大学项目,除了调用我的:forLoop方法迭代100个数字(1,1100),从1开始,一直到100,然后进行迭代%5(I%%5),其他一切都正常工作。由于某种原因,我无法让它工作。感谢任何帮助或指导 当我回显%%A时,它将遍历所有的数字。当我回显%result%时,我得到一个空白“”(里面没有任何内容) 正确的代码是Windows批处理脚本-For/L不工作--简单,windows,batch-file,variable-assignment,Windows,Batch File,Variable Assignment,需要一些快速的帮助。这是一个大学项目,除了调用我的:forLoop方法迭代100个数字(1,1100),从1开始,一直到100,然后进行迭代%5(I%%5),其他一切都正常工作。由于某种原因,我无法让它工作。感谢任何帮助或指导 当我回显%%A时,它将遍历所有的数字。当我回显%result%时,我得到一个空白“”(里面没有任何内容) 正确的代码是 :forLoop setlocal ENABLEDELAYEDEXPANSION FOR /L %%A IN (1,1,100) DO ( set /A
:forLoop
setlocal ENABLEDELAYEDEXPANSION
FOR /L %%A IN (1,1,100) DO (
set /A result=%%A %% 5
echo !result! >> results.txt
set /A total=!total!+!result!
echo !total!
)
问题是当读取的
时,%result%
被替换,这意味着在执行循环时它不再是变量。您需要的是启用延迟变量扩展,然后使用代码>而不是%
:
setlocal ENABLEDELAYEDEXPANSION
:forLoop
FOR /L %%A IN (1,1,100) DO (
set /A result=%%A %% 5
echo "%%A"
echo !result!
)
运行SET/?
时获得的帮助消息中对此进行了详细说明:
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%
延迟的环境变量扩展有助于
线路发生故障时电流扩展的限制
读取文本的一部分,而不是在执行时。下面的例子
演示立即变量展开的问题:
设置VAR=before
如果“%VAR%”之前(
设置VAR=after
如果在“@echo”后面加上“%VAR%”,如果你看到这个,它就起作用了
)
将永远不会显示该消息,因为两个IF语句中都有%VAR%
在读取第一个IF语句时被替换,因为它在逻辑上
包括IF的主体,它是一个复合语句。所以
如果在复合语句中确实比较了“before”和
“之后”永远不会相等。类似地,下面的示例
无法按预期工作:
集合列表=
对于(*)中的%i,do set LIST=%LIST%%i
回显%LIST%
因为它不会在当前目录中建立文件列表,
而是将LIST变量设置为找到的最后一个文件。
同样,这是因为当
FOR语句被读取,此时列表变量为空。
因此,我们正在执行的实际FOR循环是:
对于(*)中的%i,执行设置列表=%i
它只是将列表设置为最后找到的文件。
延迟环境变量扩展允许您使用不同的
字符(感叹号)以在以下位置展开环境变量:
执行时间。如果启用延迟变量扩展,则上述
可以编写如下示例以按预期工作:
设置VAR=before
如果“%VAR%”之前(
设置VAR=after
如果在“@echo”后面加上“!VAR!”==”,如果你看到这个,它就起作用了
)
集合列表=
对于(*)中的%i,执行设置列表=!名单!%我
回显%LIST%
知道了。(我在查看集合上的文档):-)@KenWhite显然我也应该这么做!!!!谢谢;)这对我不管用。我得到!结果
作为第二个回波的输出
。非常感谢@盖布
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%