Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
For loop 如何成批循环遍历数组?_For Loop_Batch File - Fatal编程技术网

For loop 如何成批循环遍历数组?

For loop 如何成批循环遍历数组?,for-loop,batch-file,For Loop,Batch File,我创建了如下数组: set sources[0]="\\sources\folder1\" set sources[1]="\\sources\folder2\" set sources[2]="\\sources\folder3\" set sources[3]="\\sources\folder4\" 现在我想遍历这个数组: for %%s in (%sources%) do echo %%s 它不起作用!脚本似乎没有进入循环。为什么呢?那么我如何迭代呢?这是一种方法: @echo of

我创建了如下数组:

set sources[0]="\\sources\folder1\"
set sources[1]="\\sources\folder2\"
set sources[2]="\\sources\folder3\"
set sources[3]="\\sources\folder4\"
现在我想遍历这个数组:

for %%s in (%sources%) do echo %%s
它不起作用!脚本似乎没有进入循环。为什么呢?那么我如何迭代呢?

这是一种方法:

@echo off
set sources[0]="\\sources\folder1\"
set sources[1]="\\sources\folder2\"
set sources[2]="\\sources\folder3\"
set sources[3]="\\sources\folder4\"

for /L %%a in (0,1,3) do call echo %%sources[%%a]%%

如果不需要环境变量,请执行以下操作:

for %%s in ("\\sources\folder1\" "\\sources\folder2\" "\\sources\folder3\" "\\sources\folder4\") do echo %%s

如果不知道数组有多少个元素(似乎是这样),可以使用以下方法:

for /F "tokens=2 delims==" %%s in ('set sources[') do echo %%s

请注意,元素将按字母顺序处理,也就是说,如果有9个以上(或99个等)元素,则索引必须在元素1..9(或1..99个等)中保留零个。

使用定义的另一种替代方法和不需要延迟扩展的循环:

set Arr[0]=apple
set Arr[1]=banana
set Arr[2]=cherry
set Arr[3]=donut

set "x=0"

:SymLoop
if defined Arr[%x%] (
    call echo %%Arr[%x%]%%
    set /a "x+=1"
    GOTO :SymLoop
)

确保您使用“呼叫回音”,因为除非您延迟扩展和使用,否则回音将无法工作!与我这样使用的%%

不同,重要的是变量只有1个长度,比如%%a,而不是%%repo:

for %%r in ("https://github.com/patrikx3/gitlist" "https://github.com/patrikx3/gitter" "https://github.com/patrikx3/corifeus" "https://github.com/patrikx3/corifeus-builder" "https://github.com/patrikx3/gitlist-workspace" "https://github.com/patrikx3/onenote" "https://github.com/patrikx3/resume-web") do (
   echo %%r
   git clone --bare %%r
)

为了子孙后代:我只是想对@dss提出一个小小的修改,否则答案就太棒了

在当前结构中,当您将Arr中的值分配给循环内的临时变量时,执行已定义检查的方式会导致意外输出:

例如:

@echo off
set Arr[0]=apple
set Arr[1]=banana
set Arr[2]=cherry
set Arr[3]=donut

set "x=0"

:SymLoop
if defined Arr[%x%] (
    call set VAL=%%Arr[%x%]%%
    echo %VAL%
    REM do stuff with VAL
    set /a "x+=1"
    GOTO :SymLoop
)
这实际上会产生以下不正确的输出

donut
apple
banana
cherry
最后一个元素首先打印。 要解决这个问题,更简单的方法是在处理完数组后,反转已定义的检查,使其跳过循环,而不是执行它。像这样:

@echo off
set Arr[0]=apple
set Arr[1]=banana
set Arr[2]=cherry
set Arr[3]=donut

set "x=0"

:SymLoop
if not defined Arr[%x%] goto :endLoop
call set VAL=echo %%Arr[%x%]%%
echo %VAL%
REM do your stuff VAL
SET /a "x+=1"
GOTO :SymLoop

:endLoop
echo "Done"
无论您在SymLoop中执行什么操作,都会生成所需的正确的输出

apple
banana
cherry
donut
"Done"

@这是你的小信仰。试试看。:)你说得对,我道歉!另一方面,不管数组长度如何,都可以进行动态迭代。这绝对没有帮助:/我问如何迭代数组,你给出了答案:“不要使用数组!”我只是给了你这个建议,因为有时我会根据可能的解决方案重新考虑我的程序架构。想象一下,您创建数组只是为了能够遍历项目,然后在不使用前一个数组的情况下进行迭代会使该数组变得不必要!请注意,中的
之间的空格(
很重要。这不起作用:“\\sources…”中的
但是如果您在(“\\sources…”中使用空格
会这样工作。它有点简单第一个例子中的问题就是
echo%var%
将其更改为
call echo%%var%%
并按预期工作。或者更好的是,使用delayedexpansion@jeb这是问题的症结所在,循环中的代码的行为与您在普通蝙蝠纸条中预期的不一样t、 您需要以不同的方式调用。从长远来看,这将是一个维护难题。最好确保它按照用户预期的方式工作。它在批处理脚本中的行为完全符合预期!了解这一点比绕开您不理解的构造更重要。而且您无法绕开所有括号块。