2

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?

double-beep
  • 3,889
  • 12
  • 24
  • 35
John Doe
  • 47
  • 4
  • 2
    don't put spaces around the equal sign... – npocmaka Nov 23 '18 at 21:48
  • 2
    `%port[0]%` would not exist because without a value it would be undefined. The same would also be true for `%port[3]%` after the modification. – Compo Nov 23 '18 at 21:59
  • guys thank you for pointing this out, but TBH i just showed this as an example, not the exact code =) – John Doe Nov 23 '18 at 22:02

1 Answers1

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
  • Tracking of the last element in the loop: `Last=%%L` is redundant. The last element is predetermined. – sst Nov 24 '18 at 02:56