在for for循环中提升令牌变量

时间:2015-10-28 20:07:26

标签: windows variables batch-file cmd

所以我有这段代码基本上只是一个原型,但我不能让它工作,因为for循环的tokens参数不像!var!方式定义的变量可以用于延迟扩展。

@echo on
setlocal enabledelayedexpansion
set string=4 1 5 1 6 1 6 2
set b=1
set c=2
for /L %%o in (1, 2, 8) do (
  call set /a "b=%%o"
  call set /a "c=%%o + 1"
  FOR /F "tokens=%b%,%c%" %%G IN ("%string%") DO @echo %%G %%H
)

我得到的结果是:

4 1
4 1
4 1
4 1

我想要的结果是:

4 1
5 1
6 1
6 1

我一直在试验如何解决这个问题,但无论如何都无法找到解决方案。有没有简单的方法来解决这个问题,或者我是否必须尝试使用​​单循环,也许是一些转到或调用函数?

1 个答案:

答案 0 :(得分:1)

只需呼唤一个功能。

@echo off
setlocal enabledelayedexpansion
set string=4 1 5 1 6 1 6 2
set b=1
set c=2
for /L %%o in (1, 2, 8) do (
  set /a "b=%%o"
  set /a "c=%%o + 1"
  CALL :label1 !b! !c!
)
PAUSE
GOTO :EOF


:label1
FOR /F "tokens=%1,%2" %%G IN ("%string%") DO @echo %%G %%H
GOTO :EOF
相关问题