Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Batch File: Append a string inside a for loop

Tags:

batch-file

I have the following for loop:

for /l %%a in (1,1,%count%) do (
<nul set /p=" %%a - "

Echo !var%%a!
)

which will display something like this:

1 - REL1206
2 - REL1302
3 - REL1306

I need to create a variable that appends itself based on the number of iterations. Example the variable would look like this after the for loop:

myVar="1, 2, 3"
like image 911
Brian Avatar asked Sep 03 '13 19:09

Brian


1 Answers

Use delayed expansion

setlocal enableextensions enabledelayedexpansion
SET OUTPUTSTRING=
for /l %%a in (1,1,%count%) do (
<nul set /p=" %%a - "
Echo !var%%a! 
if .!OUTPUTSTRING!==. (
    SET OUTPUTSTRING=%%a
) ELSE (
    SET OUTPUTSTRING=!OUTPUTSTRING!, %%a
)
)
SET OUTPUTSTRING
like image 69
springer38 Avatar answered Oct 23 '22 11:10

springer38