string concatenation in loop

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
moacmoa
Posts: 2
Joined: 31 Oct 2012 18:00

string concatenation in loop

#1 Post by moacmoa » 31 Oct 2012 18:07

Hello.
I need to concatenate a string in a 'for' loop.
Here is my code :

Code: Select all

set tmp=numbers
for /L %%A in (1,1,8) do (
set tmp=%tmp% %%A
)
echo %tmp%

I would like to read "numbers 1 2 3 4 5 6 7 8" but my script returns "numbers 8".

But concatenation outside of the loop works fine. This script is good :

Code: Select all

set str1=numbers
set str2=1
set str3=2
set str1=%str1% %str2%
set str1=%str1% %str3%
echo %str1%

It returns "numbers 1 2 3"

Any idea ?

Thanks

Boombox
Posts: 80
Joined: 18 Oct 2012 05:51

Re: string concatenation in loop

#2 Post by Boombox » 31 Oct 2012 19:24

.

Code: Select all

setlocal enabledelayedexpansion
set tmp=numbers
for /L %%A in (1,1,8) do (
set tmp=!tmp! %%A
)
echo !tmp!
pause


The value of TMP needs to be determined during run time.

Use delayed expansion and reference your variables with ! insead of %

moacmoa
Posts: 2
Joined: 31 Oct 2012 18:00

Re: string concatenation in loop

#3 Post by moacmoa » 01 Nov 2012 07:11

Thank you very much !

Post Reply