Problem with loop and composited new variable

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
[newbie]
Posts: 6
Joined: 07 Feb 2015 15:02

Problem with loop and composited new variable

#1 Post by [newbie] » 04 Sep 2015 06:52

Hi
I would like to add a wow_ to the old variable set "oldV=aaa bbb ccc ddd"
and the finished variable should look like set "newV=wow_aaa wow_bbb wow_ccc wow_ddd"
I get it only to:

Code: Select all

@echo off 
Setlocal EnableDelayedExpansion
set "addV=wow_"
set "oldV=aaa bbb ccc ddd"
for %%i in (%oldV%) do (
      set "newV=!addV!%%i"
      echo !newV!
      REM to new assemble...
)

How can i get the new variable composited?

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Problem with loop and composited new variable

#2 Post by foxidrive » 04 Sep 2015 07:04

Here is one way:

Code: Select all

@echo off
set "addV=wow_"
set "oldV=aaa bbb ccc ddd"
for %%a in (%oldV%) do call set "newv=%%newv%% %addV%%%a"
set "newv=%newv:~1%"
echo %newV%
pause

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Problem with loop and composited new variable

#3 Post by Squashman » 04 Sep 2015 07:05

Code: Select all

@echo off 
Setlocal EnableDelayedExpansion
set "addV=wow_"
set "oldV=aaa bbb ccc ddd"
for %%i in (%oldV%) do (
      set "newV=!newV!!addV!%%i "
      echo !newV!
      REM to new assemble...
)

[newbie]
Posts: 6
Joined: 07 Feb 2015 15:02

Re: Problem with loop and composited new variable

#4 Post by [newbie] » 04 Sep 2015 07:11

Thx4Help :)

Yury
Posts: 115
Joined: 28 Dec 2013 07:54

Re: Problem with loop and composited new variable

#5 Post by Yury » 04 Sep 2015 09:06

Code: Select all

@echo off
setlocal enabledelayedexpansion
set "oldV=aaa bbb ccc ddd"
set "addV=wow_"
set "newV=%addV%!oldV: = %addV%!"
echo %newV%

[newbie]
Posts: 6
Joined: 07 Feb 2015 15:02

Re: Problem with loop and composited new variable

#6 Post by [newbie] » 06 Sep 2015 09:44

Yury wrote:

Code: Select all

@echo off
setlocal enabledelayedexpansion
set "oldV=aaa bbb ccc ddd"
set "addV=wow_"
set "newV=%addV%!oldV: = %addV%!"
echo %newV%

Thx, very nice implementation.

Post Reply