批处理脚本打印增量编号

时间:2019-01-23 11:04:29

标签: windows batch-file

下面有一些代码可以帮助自动将choco软件包安装到repo上。

但是,我得到的只是执行从数字1开始的数字序列,而不是执行命令(正确回显了该命令)。

任何建议将不胜感激!

参考代码:

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

输出如下:

  

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332

1 个答案:

答案 0 :(得分:0)

@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

更改2行,可以避免使用代码的括号。

这有助于避免延迟扩展的需求。 set /?中显示了一个要求延迟扩展的示例。

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

括号中的所有内容均读取为1个块,并进行一次评估。因此,括号中的%VAR%不会更改,因为它已被替换为值。