How to concatenate strings in windows batch file for loop?

前端 未结 3 747
悲哀的现实
悲哀的现实 2021-02-01 12:58

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:

<         


        
3条回答
  •  -上瘾入骨i
    2021-02-01 13:40

    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" }
    

提交回复
热议问题