1

this one's a bit difficult to explain, but I'll do my best.

I'm passing a list of directories into a batch file via a string array, which is created in Java and then passed into the .bat using Runtime.getRuntime().exec(commands). The trouble I am having is in regards to accessing the commands array, the size of which may vary from execution to execution. For example, during one run, "Commands" may contain the following:

{"cmd.exe", "/C", "Start", "program.bat", "stringA", "stringB", "stringC"}

The first four elements are used to call the batch file, so only strings A, B, and C are passed into the batch file (program.bat) as parameters. However, on the next run, "commands" may look like this:

{"cmd.exe", "/C", "Start", "program.bat", "stringA", "stringB", "stringC", stringD, stringE}

As you can see, there are two more strings added to the parameters list. My question is this: In my batch file I have this:

::Get stringA (param 1)
set stringA=%1
::Get stringB (param 2)
set stringB=%2
::Get stringC (param 3)
set stringC=%3

This takes the three string parameters (from the first "commands" array) and sets local variables to whatever values are passed in to the corresponding parameters. I am wondering if there is a way to determine the number of parameters (from the second "commands" array, for example) from within the batch file, and set/create the proper number of local variables accordingly. I focus primarily on Java, so batch files are still fairly new to me. Any suggestions will be very much appreciated, as I've been trying to figure this one out for a while on my own with no success.

Rafareino
  • 2,515
  • 1
  • 19
  • 26
DerStrom8
  • 1,311
  • 2
  • 23
  • 45

2 Answers2

4
@echo off
setlocal enabledelayedexpansion

set argCount=0
for %%x in (%*) do (
   set /A argCount+=1
   set "argVec[!argCount!]=%%~x"
)

echo Number of processed arguments: %argCount%

for /L %%i in (1,1,%argCount%) do echo %%i- "!argVec[%%i]!"

For example:

C:> test One "This is | the & second one" Third
Number of processed arguments: 3
1- "One"
2- "This is | the & second one"
3- "Third"

Batch-Script - Iterate through arguments

Community
  • 1
  • 1
Aacini
  • 65,180
  • 12
  • 72
  • 108
  • Thank you, this is exactly what I was looking for. I was not aware there was a way to create arrays in a batch file, though looking back I see I should have known it was possible. – DerStrom8 Nov 14 '13 at 21:44
  • Great answer @Aacini I would like to made my answer better, but I was in commuting to my house, so I was unable to improve my answer. Now I'm feeling happier by not improving it, so me to had a chance to learn a bit more, and see great and much well organized code. Thank you! – Rafareino Nov 15 '13 at 03:39
  • @Aacini I love this snippet, allowed me to send as many `"VALUE=KEY"` pairs as I wanted, but I'm trying to adapt it to allow a `*` in the `KEY` to no avail. I posted a question here if you have a moment, or perhaps update this and I could close/link my question. https://stackoverflow.com/questions/63498946/pass-named-parameters-to-batch-script-with-special-characters – FreeSoftwareServers Aug 20 '20 at 06:15
2

As stated here:

https://stackoverflow.com/a/14298769/955143

You can read by %1 to %9, and use SHIFT to delete the first element from the array, an then renummerate they to new numbers.

For instance, the %2 becomes %1, and the 10th element becomes the 9th, and you can acess it by %9.

Since the read value become a empty string you have reached the end of the array

You can write a loop (with goto or for) to reade the %1 value, and use SHIFT to rotate the 2nd element to the first position, and check if it isn't empty, them repeat the process.

Community
  • 1
  • 1
Rafareino
  • 2,515
  • 1
  • 19
  • 26
  • I think this is exactly what I needed, which leads me to one more question--Within the loop I would have to create a variable and set it to the current value. I'm not sure how I would create a different, unique variable each time I go through the loop. – DerStrom8 Nov 14 '13 at 20:28
  • Welcome to SO. Please ask follow-up questions as their own, separate Question. – RBerteig Nov 14 '13 at 20:43
  • @RBerteig Okay, sorry about that. I will remember this for next time. – DerStrom8 Nov 14 '13 at 21:44
  • @derstrom8 when you do ask the follow up, feel free to add a link to it in your original question, or in a comment, or to link back to this question from that one. That way you will likely lure help from those who have already answered this one, and SO as a whole benefits by having clear questions with focused answers. – RBerteig Nov 14 '13 at 23:08