批处理文件:在for循环中追加一个字符串

时间:2013-09-03 19:46:59

标签: batch-file

我有以下for循环:

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

Echo !var%%a!
)

将显示如下内容:

1 - REL1206
2 - REL1302
3 - REL1306

我需要创建一个根据迭代次数追加自身的变量。示例变量在for循环后看起来像这样:

myVar="1, 2, 3"

2 个答案:

答案 0 :(得分:14)

示例:

@ECHO OFF &SETLOCAL
SET /a count=5
for /l %%a in (1,1,%count%) do call set "Myvar=%%Myvar%%, %%a"
ECHO %Myvar:~2%

..输出是:

1, 2, 3, 4, 5

答案 1 :(得分:4)

使用延迟扩展

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