How to loop through array in batch?

前端 未结 6 1493
一向
一向 2020-12-01 05:15

I created an array like this:

set sources[0]=\"\\\\sources\\folder1\\\"
set sources[1]=\"\\\\sources\\folder2\\\"
set sources[2]=\"\\\\sources\\folder3\\\"
s         


        
相关标签:
6条回答
  • 2020-12-01 05:36

    This is one way:

    @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]%%
    
    0 讨论(0)
  • 2020-12-01 05:39

    If you don't need environment variables, do:

    for %%s in ("\\sources\folder1\" "\\sources\folder2\" "\\sources\folder3\" "\\sources\folder4\") do echo %%s
    
    0 讨论(0)
  • 2020-12-01 05:43

    Another Alternative using defined and a loop that doesn't require delayed expansion:

    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
    )
    

    Be sure you use "call echo" as echo won't work unless you have delayedexpansion and use ! instead of %%

    0 讨论(0)
  • 2020-12-01 05:47

    For posterity: I just wanted to propose a slight modification on @dss otherwise great answer.

    In the current structure the way that the DEFINED check is done causes unexpected output when you assign the value from Arr to a temporary variable inside the loop:

    Example:

    @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
    )
    

    This actually produces the following incorrect output

    donut
    apple
    banana
    cherry
    

    The last element is printed first. To fix this it is simpler to invert the DEFINED check to have it jump over the loop when we're done with the array instead of executing it. Like so:

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

    This regardless of what you do inside the SymLoop always produces the desired correct output of

    apple
    banana
    cherry
    donut
    "Done"
    
    0 讨论(0)
  • 2020-12-01 05:48

    If you don't know how many elements the array have (that seems is the case), you may use this method:

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

    Note that the elements will be processed in alphabetical order, that is, if you have more than 9 (or 99, etc) elements, the index must have left zero(s) in elements 1..9 (or 1..99, etc.)

    0 讨论(0)
  • 2020-12-01 05:58

    i use like this, what is important is that the variable is only 1 length, like %%a, and not like %%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
    )
    
    0 讨论(0)
提交回复
热议问题