对于Windows命令行中的循环

时间:2014-02-18 12:34:55

标签: command-line window

如何让这个循环工作:

set list=am gp it ol

 FOR %%a IN (%list%) DO (   
     set Indxed= %%a
     ECHO %Indxed%             

)

Echo总是输出到:ol ol ol ol。我该如何解决这个问题?

2 个答案:

答案 0 :(得分:1)

测试一下:

@echo off
setlocal enabledelayedexpansion

set list=am gp it ol

FOR /f "delims=" %%a IN ("%list%") DO (
   set Indxed=%%a
   ECHO !Indxed!
)

答案 1 :(得分:0)

请尝试以下操作:

@echo off
setlocal

set list=am gp it ol

echo %list%

call :parse "%list%"

goto :eos

:parse

set list=%1
set list=%list:"=%

FOR /f "tokens=1* delims= " %%a IN ("%list%") DO (
  if not "%%a" == "" call :sub %%a
  if not "%%b" == "" call :parse "%%b"
)

goto :eos

:sub

echo %1

goto :eos

:eos
endlocal