Rearrange array elements in cmd

2019-07-18 03:06发布

问题:

I have an array in my batch file which looks like this:

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

Is there any nice and elegant way to move values one element back, so it'll look like this?:

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

I want something other than just SET port[0] = %port[1]%, etc?

回答1:

:: 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