批量FOR循环变量重复

时间:2014-05-29 06:51:42

标签: loops batch-file repeat

所以我的代码是

FOR %%a IN (a b c) DO (FOR %%b IN (x y z) DO (echo %%a %%b &pause>nul))

但输出是

ax ay az bx by bz cx cy cz

我希望每个循环中的一个变量为ax by cz并注明其他,请找到它。

4 个答案:

答案 0 :(得分:1)

@echo off
setlocal EnableDelayedExpansion

rem Define first and second arrays
set i=0
for %%a in (a b c) do (
   set /A i+=1
   set first[!i!]=%%a
)
set i=0
for %%a in (x y z) do (
   set /A i+=1
   set second[!i!]=%%a
)

rem Show elements in both arrays with the same index
for /L %%i in (1,1,%i%) do echo !first[%%i]!!second[%%i]!

没有数组的相同过程:

@echo off
setlocal EnableDelayedExpansion

set i=0
for %%a in (a b c) do (
   set /A i+=1
   set j=0
   for %%b in (x y z) do (
      set /A j+=1
      if !i! equ !j! echo %%a%%b
   )
)

答案 1 :(得分:0)

这样的事情:

@echo off
setlocal enabledelayedexpansion
set /A CNT=1
FOR %%a IN (a b c) DO (echo %%a !Cnt! &set /A Cnt+=1 &pause>nul)

答案 2 :(得分:0)

你做错了....尝试使用1 1 3的序列,其中一个是步数,这样1 1 3就会产生1 2 3 ...代码如下

@echo off
For /L %%n in (1 1 3) do (
  For %%c in (a b c) do (
    Echo %%c%%n
  )
)
Pause>null

答案 3 :(得分:0)

@echo off
setlocal enabledelayedexpansion
set "a=cat dog horse"
set "b=bad little big"
:loop
  set /a i+=1
  set "x="
  for /f "tokens=%i%" %%a in ("%b%") do set "x=%%a"<nul
  if "%x%"=="" goto :eof
  for /f "tokens=%i%" %%b in ("%a%") do ( echo %x% %%b)
goto :loop