0

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!

2 Answers 2

1

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 . . . 
Sign up to request clarification or add additional context in comments.

1 Comment

Unfortunately, this won't work if there is a * in one of the header variables.
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.