Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to concatenate strings in windows batch file for loop?

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.

like image 360
Marcus Thornton Avatar asked Jul 19 '13 10:07

Marcus Thornton


People also ask

How do you concatenate in a loop?

Python concatenate strings in for loop To concatenate strings we will use for loop, and the “+ ” operator is the most common way to concatenate strings in python. To get the output, I have used print(my_str).

How do I loop a batch file in Windows?

Pressing "y" would use the goto command and go back to start and rerun the batch file. Pressing any other key would exit the batch file.

What does %1 mean in a batch file?

When used in a command line, script, or batch file, %1 is used to represent a variable or matched string. For example, in a Microsoft batch file, %1 can print what is entered after the batch file name.


2 Answers

Try this, with strings:

set "var=string1string2string3" 

and with string variables:

set "var=%string1%%string2%%string3%" 
like image 120
Endoro Avatar answered Sep 22 '22 21:09

Endoro


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" } 
like image 28
Ansgar Wiechers Avatar answered Sep 21 '22 21:09

Ansgar Wiechers