Abhi
Abhi

Reputation: 177

Passing Array as an argument in batch script

I have created an array in batch script as:

set arr[1]=a
set arr[2]=b
set arr[3]=c

Now I want to pass this array as an argument to another batch file as follows:

call :processArr.bat arr

I need to do this because in actual the value of %%i in arr[%%i] is variable and it can be greater than 9 and with batch file only 9 arguments can be passed

Moreover its ultra essential that the entire array is passed to the batch file processArr.bat at once

Please help

Upvotes: 2

Views: 6452

Answers (1)

rojo
rojo

Reputation: 24466

There's no need to pass the variables as script arguments. When you call processArr.bat, processArr.bat will inherit all the variables defined by the calling script. Here's a demonstration:

test.bat:

@echo off & setlocal

for /L %%I in (0,1,5) do set /a "arr[%%I] = %%I << 2"

call test2.bat

test2.bat:

@echo off & setlocal

echo Checking inheritance...
set arr

Output:

Checking inheritance...
arr[0]=0
arr[1]=4
arr[2]=8
arr[3]=12
arr[4]=16
arr[5]=20

Upvotes: 4

Related Questions