简体   繁体   中英

Batch Script printing incremental numbers

I have some code below which is meant to help automate installing choco packages onto a repo .

However, instead of executing the command (which echos out correctly), all I get is a sequence of numbers starting from the number 1.

Any advice would be greatly appreciated!

Code for reference:

@echo off 
set arr[0]=sts
set arr[1]=winscp 
set arr[2]=tortoisegit
set arr[3]=office2013pro  
set arr[4]=notepadplusplusandpm
set arr[5]=git
set arr[6]=GoogleChrome
set arr[7]=jdk
set arr[8]=maven
set "x=0"

:SymLoop  
if defined arr[%x%] (
    call set entry=%%arr[%x%]%%
    set command=choco install %entry% -y
    REM Command isn't running, just prints off numbers
    %command%
    set /a "x+=1"
    GOTO :SymLoop
)

Output is below:

@echo off
set arr[0]=sts
set arr[1]=winscp
set arr[2]=tortoisegit
set arr[3]=office2013pro
set arr[4]=notepadplusplusandpm
set arr[5]=git
set arr[6]=GoogleChrome
set arr[7]=jdk
set arr[8]=maven
set "x=0"

:SymLoop
if not defined arr[%x%] goto :EndLoop
call set "entry=%%arr[%x%]%%"
set "command=choco install %entry% -y"
REM Command isn't running, just prints off numbers
%command%
set /a "x+=1"
GOTO :SymLoop
:EndLoop

Changing 2 lines and you can avoid the parentheses block of code.

This helps to avoid the need the delayed expansion. An example of requiring delayed expansion is shown in set /? .

 set VAR=before if "%VAR%" == "before" ( set VAR=after if "%VAR%" == "after" @echo If you see this, it worked ) 

Everything between the parentheses is read as 1 block and evaluated once. So %VAR% within the parentheses does not change as it already has been substituted with the value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM