1

I am working on a batch file. I have an array:

setlocal enabledelayedexpansion
set Base_list[0]=Base1
set Base_list[1]=Base2
set Base_list[2]=Base3
set Base_list[3]=Base4
set Base_list[4]=Base5
set Base_list[5]=Base6
set Current_Node=Node1
if "%Current_Node%" == "Node1" ( for /l %%a in (0 ,%counter% , %Base_list%) do (
// do some stuff
)
)
  1. How do I print this array named Base_list?

  2. How can I iterate through this array?

Raptor
  • 53,206
  • 45
  • 230
  • 366
golu kashyap
  • 213
  • 2
  • 5
  • 10

3 Answers3

1

You have a couple minor errors in your code:

  • You have not initialized the counter variable.
  • The format of FOR command is slightly different
  • A comment is inserted via REM command, not //

This is the right version of your code:

@echo off
setlocal enabledelayedexpansion
set Base_list[0]=Base1
set Base_list[1]=Base2
set Base_list[2]=Base3
set Base_list[3]=Base4
set Base_list[4]=Base5
set Base_list[5]=Base6
set Current_Node=Node1

rem Explicitly set the counter:
set counter=5

if "%Current_Node%" == "Node1" (
   for /l %%a in (0 , 1, %counter%) do (
   rem do some stuff
   echo Element %%a in Base_list array is: !Base_list[%%a]!
   )
)

However, this code may be written in a simpler way:

@echo off
setlocal enabledelayedexpansion

rem Create the array via elements placed in a FOR command
rem at same time, generate the counter:
set counter=0
for %%a in (Base1 Base2 Base3 Base4 Base5 Base6) do (
   set Base_list[!counter!]=%%a
   set /A counter+=1
)

rem Adjust the counter because the array is zero-based:
set /A counter-=1

set Current_Node=Node1

if "%Current_Node%" == "Node1" (
   for /l %%a in (0 , 1, %counter%) do (
   rem do some stuff
   echo Element %%a in Base_list array is: !Base_list[%%a]!
   )
)

I suggest you to read this post on array management in Batch files.

Community
  • 1
  • 1
Aacini
  • 65,180
  • 12
  • 72
  • 108
0

Print:

set base_list[

(This assumes you don't want to keep numeric sequence if the index >9 and there are no unwanted variables with names starting base_list[ - you don't specify)

Iterate:

for /f "tokens=1*delims==" %%a in ('set base_list[ 2^>nul') do echo %%b

or

for /f "tokens=1*delims==" %%a in ('set base_list[ 2^>nul') do echo %%a has value %%b

depending on quite what you want to do - same conditions as "print".

Magoo
  • 77,302
  • 8
  • 62
  • 84
-1

I think, you are completely confused by for /L.

The correct Syntax is for /L %%i in (<start>,<step>,<end>) do...

@echo off
setlocal enabledelayedexpansion
set Base_list[0]=Base1
set Base_list[1]=Base2
set Base_list[2]=Base3
set Base_list[3]=Base4
set Base_list[4]=Base5
set Base_list[5]=Base6
set Current_Node=Node1

REM list array:
set base_list[

REM list values only:
for /f "tokens=2 delims==" %%i in ('set Base_list[') do echo %%i

REM get size of the array:
for /f %%i in ('set base_list[ ^|find /c "="') do set count=%%i
echo size is %count%
REM subtract one because array starts with 0:
set /a count-=1

if "%Current_Node%" == "Node1" for /l %%a in (0,1,%count%) do (
  echo  %%a: some stuff with !base_list[%%a]!
)
Stephan
  • 53,940
  • 10
  • 58
  • 91