开发者

How to split strings by in loop of batch

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 outp开发者_如何学编程ut "appdef.h", confusing!


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 . . . 


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜