在 for 循环中初始化的批处理文件变量 [英] Batch file variables initialized in a for loop

查看:33
本文介绍了在 for 循环中初始化的批处理文件变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个批处理文件,它通过 for 循环内的 SET 为磁盘上的一组文件初始化变量:

I have a batch file which initializes variables via SET inside a for loop, for a set of files on disk:

for %%f in (%MYTARGETDIR%*config.xml) do (
  SET TMPFILE=%%F.tmp

  echo In loop %TMPFILE%
)

echo End loop %TMPFILE%

当我在全新的命令 shell(未定义 TMPFILE)中运行它时,循环内回显为空,但结束循环回显不是.

when I run this in a brand new command shell (without TMPFILE defined) the In loop echo is empty, but the end loop echo is not.

当我第二次运行时,它已经设置,并输出它的值,忽略设置直到循环关闭.

When I run a second time, its already set, and outputs its value, ignoring the set until the loop closes.

为什么会这样,以及解决方法是什么?

Ideas why this is, and what the workaround is?

推荐答案

那是因为 ( ) 块只被解析一次,它在运行循环之前替换了 %TMPFILE%.

That's because the ( ) block is parsed only once, it substitutes %TMPFILE% before it runs your loop.

这篇关于在 for 循环中初始化的批处理文件变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆