批处理文件循环遍历URL数组

时间:2014-03-19 18:51:47

标签: url batch-file command command-line-arguments

我正在尝试遍历URL列表并继续遇到麻烦。具体来说,由于等号,我一直遇到参数问题。

set  loadval[1]="/orders/counts"
set  loadval[2]="/orders/counts?aggregationKind=Day&fullHistory=true"
set  loadval[3]="/products/popularity?aggregationKind=Month&aggregationCount=12"
set  loadval[4]="/products/popularity/aggregated?aggregationKind=Month&aggregationCount=12"

for /F "tokens=4 delims==" %%s in ('set loadval[') do (
  echo    Connecting to %%s
)

想法?

2 个答案:

答案 0 :(得分:4)

您的tokens=4值错误。在set loadval[命令中,显示的值与这些值类似:

loadval[1]="/orders/counts"
loadval[2]="/orders/counts?aggregationKind=Day&fullHistory=true"
loadval[3]="/products/popularity?aggregationKind=Month&aggregationCount=12"
loadval[4]="/products/popularity/aggregated?aggregationKind=Month&aggregationCount=12"

如果您想在等号之后处理值,那么在第一个之后的标记=分隔:

for /F "tokens=1* delims==" %%s in ('set loadval[') do (
  echo    Connecting to %%t
)

答案 1 :(得分:0)

这是我做的:

setlocal enabledelayedexpansion

set  loadval[1]="/orders/counts"
set  loadval[2]="/orders/counts?aggregationKind=Day&fullHistory=true"
set  loadval[3]="/products/popularity?aggregationKind=Month&aggregationCount=12"
set  loadval[4]="/products/popularity/aggregated?aggregationKind=Month&aggregationCount=12"

for /L %%a in (1,1,4) do (
  call echo     Connecting to !loadval[%%a]!
)

希望这有助于其他人。