tags:

views:

44

answers:

2

Generally, i want do do some operations to some vars in a loop, the number of vars may change, i tried a sample like this:

set header1=appdef1.h
set header2=appdef2.h
set header3=appdef3.h
set headers= "%header1% %header2% %header3%"
for /f "delims= " %%i in (%headers%) do echo %%i
pause

while it only output "appdef.h", confusing!

+1  A: 

Got the solution:

@echo off
set header1=appdef1.h
set header2=appdef2.h
set header3=appdef3.h
set headers= "%header1% %header2% %header3%"

for /f "tokens=* delims= " %%i in (%headers%) do call :lable %%i
goto END

:lable
if "%1"=="" (
echo the end
goto END
)

echo %1
shift
goto lable

:END
pause

Hyden
+1  A: 

This should do what you want:

set header1=appdef1.h
set header2=appdef2.h
set header3=appdef3.h
set headers=%header1% %header2% %header3%
for %%i in (%headers%) do echo %%i
pause

Your original code passed just the 1 parameter as %%i, your fix passed them as %1, %2 %3 to :lable, which then split them out.

My code calls echo %%i once with each parameter.

c:\>set header1=appdef1.h 

c:\>set header2=appdef2.h 

c:\>set header3=appdef3.h 

c:\>set headers=appdef1.h appdef2.h appdef3.h 

c:\>for %i in (appdef1.h appdef2.h appdef3.h) do echo %i 

c:\>echo appdef1.h 
appdef1.h

c:\>echo appdef2.h 
appdef2.h

c:\>echo appdef3.h 
appdef3.h

c:\>pause
Press any key to continue . . . 
Andy Morris