[英]Windows Batch: acessing variables from inside FOR loop
Im trying to do this rather elementary thing in a DOS batch: 我正在尝试在DOS批处理中执行以下基本操作:
@echo off
set _sets=^
RO:111:Rondonia,^
AC:112:Acre,^
AM:113:Amazonas,^
RR:114:Roraima
set _family_name=MyFamily
FOR %%i IN (%_sets%) DO (
echo %%i ----- %_family_name%
)
Output: 输出:
RO:111:Rondonia -----
AC:112:Acre -----
AM:113:Amazonas -----
RR:114:Roraima -----
After the ----- is supposed to appear "MyFamily", but instead nothing appears. 在-----之后应该出现“ MyFamily”,但是什么也没有出现。
How do I access variables set outside a FOR loop from within it? 如何从FOR循环内部访问在FOR循环外部设置的变量? I have no idea why the _family_name variable is not visible inside the for loop.
我不知道为什么在for循环中看不到_family_name变量。 I'm somewhat new to batch scripts.
我对批处理脚本有些陌生。 I'm used to C++ and Java programming, thus most likely my thinking does not apply to the batch realm.
我习惯于C ++和Java编程,因此很可能我的想法不适用于批处理领域。
I also need to split the string triplets "AA:NNN:AAAAAA" into three individual variables. 我还需要将字符串三元组“ AA:NNN:AAAAAA”分成三个单独的变量。 I tried to come up with a nested for loop, but I couldn't tackle this problem.
我试图提出一个嵌套的for循环,但是我无法解决这个问题。
The example was made simple for clarity. 为了清楚起见,使该示例变得简单。 The actual Batch is more complex than that.
实际的批处理比这更复杂。 I have to access 10-12 variables from within the loop.
我必须从循环中访问10-12个变量。 So please consider this aspect before answering.
因此,请在回答之前考虑这方面。 Thanks in advance.
提前致谢。
If _family_name
is actually being set within another (outer) loop, you'll need to enable delayed expansion with something like this: 如果
_family_name
实际上是在另一个(外)循环中设置的,则需要启用延迟扩展,如下所示:
@echo off
setlocal enabledelayedexpansion
for ...
set foo=bar
for ...
echo !foo!
)
)
Note that you access the variable with !exclamations!
请注意,您可以使用
!exclamations!
访问变量!exclamations!
instead of %percents%
there. 而不是那里的
%percents%
。
See if this helps you: 看看这是否对您有帮助:
FOR /f %%a "tokens=1-4 delims=," IN ("%_sets%") DO (
echo %%a ----- %_family_name%
echo %%b ----- %_family_name%
echo %%c ----- %_family_name%
echo %%d ----- %_family_name%
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.