在cmd中重新排列数组元素

时间:2018-11-23 21:39:17

标签: batch-file cmd

我的批处理文件中有一个数组,如下所示:

"port[0] ="
"port[1] = 0"
"port[2] = 3"
"port[3] = 2"

是否有任何精美而优雅的方法将值移回一个元素,所以看起来像这样?

"port[0] = 0"
"port[1] = 3"
"port[2] = 2"
"port[3] ="

除了SET port[0] = %port[1]%等以外,我还想要其他东西吗?

1 个答案:

答案 0 :(得分:2)

:: Q:\Test\2018\11\23\SO_53453204.cmd
@Echo off&SetLocal  EnableDelayedExpansion
set "port[0]="   &Rem this clears/deletes the variable
set "port[1]=0"
set "port[2]=3"
set "port[3]=2"

For /L %%L in (1,1,3) do (
  set /A "New=%%L-1,Last=%%L"
  set "port[!New!]=!port[%%L]!"
)
:: finally reset the last entry
set "port[%Last%]="
set port[

> Q:\Test\2018\11\23\SO_53453204.cmd
port[0]=0
port[1]=3
port[2]=2