如何串联Windows批处理文件中的字符串进行循环? [英] How to concatenate strings in windows batch file for loop?

查看:266
本文介绍了如何串联Windows批处理文件中的字符串进行循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我熟悉Unix shell脚本,但是Windows脚本的新手.

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

我有一个包含str1,str2,str3 ... str10的字符串列表.我想这样:

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

我确实找到了一些描述如何在批处理文件中连接字符串的线程.但是它某种程度上在for循环中不起作用.所以我仍然对批处理语法感到困惑.

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.

推荐答案

可以批量执行以下操作:

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!"
)

如果循环中其他位置不需要变量!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"

但是,与C.B.一样,我尽可能使用PowerShell:

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
}

同样,如果您在循环中的其他位置不需要$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" }

这篇关于如何串联Windows批处理文件中的字符串进行循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆