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

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

问题描述

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

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.

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

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