0

I am trying to call same batch file X times (in this code its 3) while passing a variable that is from a list that I cycle through. So in this case the list is (I J M) so I wish to call the batch file X times passing I then J then M then I then J then M and so on.

My current code is this:

@echo off

set letters=I J M

set /a dl = 0

setlocal enabledelayedexpansion

for %%i in (%letters%) do (
  set /a dl+=1
  set ltrs[!dl!]=%%i
)

set /a lCount = 0

for /l %%x in (1, 1, 3) do (

   start "RBatch%%x" cmd /k call RBatch.bat !ltrs[!lCount!]!
   set /a lCount+=1
   if !lCount! geq !dl! set /a lCount = 0
   
)

PAUSE

The RBatch.bat file is just:

echo %1
PAUSE

Currently RBatch.bat just displays lCount...

What am I doing wrong?

Thanks

Pearl
  • 392
  • 2
  • 12
  • 1
    Change `!ltrs[!lCount!]!` to `!ltrs[%%x]!` – Squashman May 06 '21 at 02:20
  • That is going to make it cycle through the file count not the letters though – Pearl May 06 '21 at 09:43
  • 1
    It does that because your code is read as two undefined variables either side of a string, i.e. `!ltrs[!`, and `!]!` expand to nothing, then all that is left is the string `lCount`. – Compo May 06 '21 at 09:57
  • I do not understand why they are undefined in the loop? – Pearl May 06 '21 at 10:03
  • You cannot do double delayed expansion to access an array variable. If you use my method the variable will be `I` the first time the loop executes. Then `J` on the second iteration and so one. I am not sure why you have the counter inside the `FOR /L` group because the loop itself is counting for you. – Squashman May 06 '21 at 12:34
  • But this still begs the question why you are not just doing this all with one `FOR` command? `for %%i in (%letters%) do start "RBatch%%i" cmd /k call RBatch.bat %%i` – Squashman May 06 '21 at 12:36
  • Basically I want to launch X number of windows(14 actually) and cycle through the letters on each launch IE 1-I, 2-J, 3-M, 4-I, 2-J etc – Pearl May 07 '21 at 03:34

0 Answers0