2

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

2
  • You are not limited to passing 9 arguments. You can use the shift command to access the other arguments. Commented Dec 5, 2017 at 13:32
  • And get rid of the colon in call processArr.bat args. The colon is for calling labels within the same script, not for executing external scripts. Commented Dec 5, 2017 at 13:33

1 Answer 1

4

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

Sign up to request clarification or add additional context in comments.

7 Comments

OMG! I wonder what would have happened to programming noobs like me if you guys weren't there! thnx a lot
Another question regarding this, if I convert both of these batch files to .exe for security reasons, does the variable inheritance still remain valid??
@Abhi Wrapping a .bat script in an .exe doesn't strike me as secure. As soon as the .exe is run, the encapsulated .bat script gets extracted to %temp% and can be read by anyone. As for whether inheritance remains, I couldn't say. Just try it and see I guess.
okay. So the batch file from %temp% dir is the one which is actually running? basically my script takes input as username and password from the user and does a couple of activities involving two other batch files which I "start xyz.bat(.exe in case I convert)" from main batch script. My intention is once the main batch file is started no one can attempt to display the password on screen by using echo by editing the other batch files. Is there any better way to prevent knowing the password ?
You could use salted hashes instead of plain text passwords. See this page for an example. This is still of trivial security, but it's at least marginally better than dealing with a plain text password I think.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.