75

I'm familiar with Unix shell scripting, but new to windows scripting.

I have a list of strings containing str1, str2, str3...str10. I want to do like this:

for string in string_list
do
  var = string+"xyz"
  svn co var
end

I do found some thread describing how to concatenate string in batch file. But it somehow doesn't work in for loop. So I'm still confusing about the batch syntax.

1

3 Answers 3

66

Try this, with strings:

set "var=string1string2string3"

and with string variables:

set "var=%string1%%string2%%string3%"
Sign up to request clarification or add additional context in comments.

2 Comments

Hint: careful about NO space between var and =
how about mixing strings with string variables?
49

In batch you could do it like this:

@echo off

setlocal EnableDelayedExpansion

set "string_list=str1 str2 str3 ... str10"

for %%s in (%string_list%) do (
  set "var=%%sxyz"
  svn co "!var!"
)

If you don't need the variable !var! elsewhere in the loop, you could simplify that to

@echo off

setlocal

set "string_list=str1 str2 str3 ... str10"

for %%s in (%string_list%) do svn co "%%sxyz"

However, like C.B. I'd prefer PowerShell if at all possible:

$string_list = 'str1', 'str2', 'str3', ... 'str10'

$string_list | ForEach-Object {
  $var = "${_}xyz"   # alternatively: $var = $_ + 'xyz'
  svn co $var
}

Again, this could be simplified if you don't need $var elsewhere in the loop:

$string_list = 'str1', 'str2', 'str3', ... 'str10'
$string_list | ForEach-Object { svn co "${_}xyz" }

Comments

17

A very simple example:

SET a=Hello
SET b=World
SET c=%a% %b%!
echo %c%

The result should be:

Hello World!

3 Comments

If you add a more or less duplicate answer, it should at least work (It doesn't, the output is !)
Please try to run it via cmd, not power shell.
The question is regarding concatenating strings in a loop. Your answer does not apply within a loop? Therefore, the answer is irrelevant to the original question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.